1 Comments
Sinatra Framework Logo

As web developer you might have heard of Sinatra. Not the famous artist, but the Sinatra framework! It is a framework/Domain Specific Language (DSL) for Ruby to quickly create web applications with minimal effort.

Nancy is a lightweight framework which is inspired by Sinatra. It has been around since November 2010 and a few days ago there was the Nancy 0.12 release.

So it's time to give this a try and make my own first Nancy web application.

My first Nancy web app

Launch visual studio and File -> New project -> ASP.NET Empty Web application

File new Asp.Net empty web app project

Right click on the project in the solution explorer and select ‘Manage NuGet packages’

Solution explorer

Type in Nancy.Hosting.Aspnet This NuGet package depends on Nancy 0.12 which is automatically installed when you hit the Install button

Nancy Nuget package

Add a new class HelloNancy.cs

Make it inherit NancyModule

type in the default constructor

Get["/"] = parameters => "Hello World";

And don’t forget to add

using Nancy;

Your full code of HelloNancy.cs should look something like

using Nancy;

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

And hit F5 to build

This gives you a nice and clean hello world:

hello world in firefox

If you add (below the previous Get…) this line:

Get["/hello/{name}"] = parameters => "Hello " + parameters.name;

 

And go to /hello/yourname

hello world in firefox with parameter

Nancy is build for the web in a developer friendly way. Now you use HTTP GET requests, but you can also use the POST and DELETE. The routing can work with regular expressions and is very powerful.

But besides the query string, you can also accept JSON, XML etc. out of the box with a single line of code!

But this sample does not contain any HTML! Well you can do that by adding a view. So add a folder called views and add a products.html file.

When you add this line:

Get["/products"] = parameters => View[new ProductsModel()];

and this class to your solution:

public class ProductsModel
{
    public string Code { get; set; }
    public string Name { get; set; }
}

Nancy will look in the views folder for a view called products.

When you use @!Model.Code in your products.html you can show the bound value of the productsmodel object in html.

Get["/products"] = parameters => View["products.html", new ProductsModel(){Code="C1009", Name ="Product name here"}];

You can read more about it here:

https://github.com/NancyFx/Nancy/wiki/Documentation

You can also download my small sample project from Github.

Nancy is open source under an MIT license and available on GitHub. So you can fork it and make pull requests etc.

Good luck coding!

 

ps. if you want to see some octocat’s (Github logo) check out this Octodex!

kick it on DotNetKicks.com Shout it

Pin on pinterest Plus on Googleplus Post on LinkedIn

Comments

Comment by DotNetKicks.com

My first Nancy webapplication

You've been kicked (a good thing) - Trackback from DotNetKicks.com