NancyFx – The Overly Simplified Framework for creating Web Applications

Being a Software developer, it’s our prime duty to simplify others life by automating various processes but have you ever wondered who will simplify our life, the software developer’s life? …No don’t take me wrong here! I know there are various IDEs which simplify our life by providing us Intelli-sense options (I know the pain when Intelli-sense doesn’t work properly and you need to remember exact syntax while writing the code…ohh god! Developer’s biggest nightmare), also there are various frameworks which handles things internally and we just need to do a configuration. But, writing lots of configuration is still a pain isn’t it? And the list goes on….

But here comes a good news, creating web applications in .Net framework is easy now! Oh, come on!!! This is already a simple task, Thanks to ASP.NET Web API. So what’s new in it? …..Folks, hold on your horses! I am not talking about ASP.NET Web API here. There is a new member in a league, The NancyFx!

So what’s so special about this framework? Well there are many things……! Nancy promotes what they call the “super-duper-happy-path.” That means things should just work, they should be easy to customize, your code should be simple and Nancy itself shouldn’t get in your way.

Let me give you a little background about NancyFx. It is an open source project and code can be found at https://github.com/NancyFx/Nancy. On their site, they documented various aspects quite nicely. This framework is inspired by Ruby’s “Sinatra” framework. The ASP.Net framework is somewhat too heavy to handle, so Nancy is definitely made for MVC developers and can develop extremely fast web applications. Nancy is a light weight framework or micro web framework for building HTTP based services on .Net (also Mono). Right from the start, Nancy was designed to not have any dependencies on existing frameworks and it is built to run anywhere. Cool, isn’t it?

I came to know about this framework recently and I thought why not to explore it more. In this article I will give an example of how to create simple web api using NancyFx. So let’s start:

(Note: I am using Visual Studio 2012 for creating the application.)

Create a “Console Application” in Visual Studio.Console App

Yes, I am not kidding! You will be creating WebApi using a console application. It’s not the preferred way of doing things but for simplicity I chose console application and you can see the power of Nancy with this. 🙂

To create application using NancyFx, we will need to install Nancy Nuget packages.Adding Nancy NuGet

Once the Nancy package is installed, we will create a Module that will handle the request to Web Api. Nancy Modules are bit like a controller (/WebApiController) in ASP.Net MVC (/Web Api) still keep in mind that Nancy can do much more than MVC. We can keep modules anywhere, but for proper segregation of components let’s create a “Modules” folder and create a module inside it. A module is nothing but a “Public class” inheriting “NancyModule” as shown below.Nancy module creation

Routes are defined in the constructor of a module. In order to define a route in Nancy, you need to specify a Method + Pattern + Action + (optional) Condition.Route defining in Nancy

In above code, “Get” is method. Nancy supports the following methods  DELETE,  GET,  HEAD,  OPTIONS,  POST,  PUT and PATCH. Then we are specifying pattern which declares the application-relative URL that the route answers to. An Action is the behavior which is invoked when a request is matched to a route. Here we are just returning strings through each action of route.

So with this step we are done with basic web api creation in Nancy but how to host it? Well Nancy applications can be hosted through various options but here I will be demonstrating the self-hosting power of Nancy as we are creating the Web Api using console application. For that we will need to add a NuGet package as shown below.Nancy host NuGet

Once the package is installed we will write a code to self-host the application. For this task NancyHost will be used. We will write this code in Program.cs file in our console application, so the moment console application will get executed, it will host our web apis at specified location.Nancy host code

NancyHost will be hosting our web apis at http://127.0.0.1:1234 url as written in code above.

Now press F5 to run the console application and AHAAAAA……!!Console application

Let’s check if our Apis are running properly or not by opening http://127.0.0.1:1234 url in web browser.root of application

If you try url http://127.0.0.1:1234/index then you will see following output.route with index

And if you want to check if third api is working or not then specify any name after the url such as http://127.0.0.1:1234/Gauriroute with parameter

 

Congratulations you have created simple apis with NancyFx! Hope this article has given you a small glimpse of how Nancy works. It’s just a start and I will explore it more.

 

Till then keep learning and do share your opinion on NancyFx in comment box below. I would love to read them.