I inherited a WCF project containing some badly constructed routes (which work) that I need to migrate into a new NancyFx project. I need to be able to define routes in my Nancy app that respond to the same GET requests. I cannot change the routes for the time being.
In the WCF project, a GET request to …
http://localhost:12345/webapi/GetUsers?UserId=567&Language=en
matches this UriTemplate:
UriTemplate = "GetUsers?UserId={userId}&Language={language}
I was hoping this would be the equivalent in Nancy
Get["/GetUsers?UserId={userId}&Language={language}"] = p => { ... }
but the same GET request results in a 404.
Is there a way to structure my Nancy route to respond to this GET request? If not, any work-arounds?
I know this is horrible but its temporary until I can schedule time with our UI team to rewrite the front-end to call proper rest-full URLs.
John,
Your route would be
/GetUsers(how are you handling the/webapipart by the way? Is that the base url of your application or are you setting up a module path?) and you would read the query-string using theRequest.Querymember.The Query property returns a
DynamicDictionarywhich enables you to access the values as either propertiesRequest.Query.UserIdor as a dictionaryRequest.Query["UserId"]You cannot make the query-string part of pattern that needs to be matched in order for the routes to be invoked. What you can do, if you really want to, is to use the route condition, which is the second parameter on the route declaration. This gives you control over a predicate which determines if the route is OK to be used or not. So you could do something like this
You could then refactor it all to an extension method, on NancyContext, that makes it a bit tidier
and make the extension something like
Hope this helps!