Gareth Hughes > Nancy

This website uses cookies to improve user experience. By continuing to use this website I assume that you are okay with this.

Nancy


23 February 2012

Nancy is a Micro .NET framework inspired by Sinatra and is available on Nuget. I decided that it was time to rewrite this site. The previous version of this site was made using ASP.NET MVC, NHibernate and Castle Windsor. The new version uses Nancy, Simple.Data and TinyIoC. 

Rather than using controllers Nancy applications are made from modules with routes and action. The first example in the docs is:

public class HelloModule : NancyModule
{
    public HelloModule()
    {
        Get["/"] = parameters => "Hello World";
    }
}

It's a typical Hello World example but you can use Razor or Spark with this and the routing can be any regular expression mapped to parameters which get passed through to the action as a dynamic object. The actions are of type Func<dynamic, Response>. 

You can also specify a condition which is type Func<NancyContext, bool>.

You can do more complex routes such as the following to match an id which is a digit and a slug which are then passed to BlogPage. CheckBlogExists does logic to make sure that the blog exists before it loads.

Get[ @"/(?<id>[\d]+)/{slug}", CheckBlogExists ] = p => BlogPage( p );

Nancy currently doesn't pass the params through to the condition, my fork of Nancy has been patched to do that because it suited my needs more. I've raised it as an issue with the Nancy devs but it's currently by design for performance.

comments powered by Disqus

© Gareth Hughes 2013 — gareth@grhughes.com — +61481554521