If you are using NancyFX, likely at some point you needed to generate a URI of another route in your application.
When I first ran into this problem, I was looking for some type of built-in URI builder, similar to what you would do with ASP.NET MVC’s Url.Action().
Simple URI builder for named Nancy routes with optional pass through of query parameters.
It’s really that simple. What it allows you to do is generate URI’s based on the names given to your routes.
If you are not naming your routes yet, you will need to do so. This is fairly straightfowrad, and you really only need to do it if you need to use the linker.
First thing we need to do is acquire the Nancy.Linker package:
PM> Install-Package Nancy.Linker
Now here is an example of an route without a defined name:
To give this route a name, simply supply it as the first argument of the route builder.
In order to build a URI, we must take a dependency on IResourceLinker in our Nancy Module. I was unable to have IResourceLinker automatically register with my container, so I’ve registered it in my Boostrapper.
Now we we can generate a URI to a named route.
In my example, I’m going to create another route that is simply going to return the URI of our existing route named “HelloWorldRoute”.
Now if we access http://localhost:5000/findhelloworld via Postman, our results as expected.
Often your routes will have parameters in which you will need to pass to the linker. I will modify the above example to include a string route parameter.
The source code for this demo is available on Github.