Using Nancy.Linker with Views

TL;DR

You have two options: 
  • The simplest is to use Nancy.Linker in your route handler to generate the links needed in the views, put them on the view model and pass the view model to the view as usual. 
  • The other is to pass IResourceLinker to the view and allow it to generate links as needed. For this to work you may need a little bit of web.config'ing to make Razor play nice. 
This post shows the former.

Nothing New

In the last post I introduced Nancy.Linker, showed how to use it to create links to named routes and place them on a model object returned by a Nancy route handler.

In essence; given this module:


This route handler will return a model with a link to the route in the module above in either XML, JSON or whatever other format you have support for in your application:


(how the format is chosen, and which are supported is another story)
Now, if you want to show that link in a view, you just have to add one line the to handler and - of course - view code. The handler becomes:


Assuming you are using Razor for your views the bar.cshtml view can simply be:


There you have it. Just use Nancy.Linker the same as when returning data, point to the view from the handler and use the generated link as any other string passed to a view.

This is the approach I'd recommend, but in the next post I will show how to use IResourceLinker in Razor code.

"This post was aggregated from https://www.horsdal-consult.dk/2014/07/using-nancylinker-with-views.html and all comments should be submitted on the original post"