Skip to content

Nancy.Linker: NancyFX URI Builder

Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


NancyFXIf 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().

Nancy.Linker

Luckily, I found Nancy.Linker by Christian Horsdal.
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.

Usage

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. Nancy

Route Parameters

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.

Source Codegithub-octocat

The source code for this demo is available on Github.

1 thought on “Nancy.Linker: NancyFX URI Builder”

  1. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #2018

Leave a Reply

Your email address will not be published. Required fields are marked *