Capture all URL segments after the initial match with Nancy

I would like to have a nancy rule that matches / captures all segments of the URL after the initial match.

For example, I would like to do this:

have a url: / views / viewname / pageid / visitid / someother

and a rule like this:

Get["/views/{view}/{all other values}"] = parameters => { string view = parameters.view; List<string> listOfOtherValues = all other parameters.. return ... }; 

listOfOtherValues ​​in summary:

  • pageid
  • visitid
  • someother

I would also like to do this for query parameters.

given the url: / views / viewname? pageid = 1 & visitid = 34 & someother = hello

then listOfOtherValues ​​will eventually be:

  • 1
  • 34
  • Hello

Is this even possible with Nancy?

+4
source share
1 answer

For the first problem, you can use regex as well as simple names to define capture groups. This way you simply determine the catch of all RegEx.
For your second, you just need to list through the Request.Query dictionary.

Here is some code that demonstrates how in one route.

 public class CustomModule : NancyModule { public CustomModule() { Get["/views/{view}/(?<all>.*)"] = Render; } private Response Render(dynamic parameters) { String result = "View: " + parameters.view + "<br/>"; foreach (var other in ((string)parameters.all).Split('/')) result += other + "<br/>"; foreach (var name in Request.Query) result += name + ": " + Request.Query[name] + "<br/>"; return result; } } 

Using this, you can call a URL, for example /views/home/abc/def/ghi/?x=1&y=2 , and get the output View: home
abc
def
ghi
x: 1
y: 2

Note: foreach over Request.Query - support in version v0.9 +

+4
source

All Articles