NancyFX provides an elegant web framework for ASP.NET developers

Diem Do

NancyFX offers a simple, straightforward approach to building and deploying web applications. This walk-through shows how to set up and start using NancyFX. 

 

microsoft-net-framework-4-5.jpg

 

As a .NET developer continuing to learn and use Ruby on a daily basis, I am often surprised to see the influence the latter has on the former. NancyFX is a good example, as it brings the Ruby Sinatra web framework to the .NET world. (Singer Frank Sinatra’s daughter is named Nancy, so isn’t the name NancyFX clever?) It provides a concise and expressive framework for building web applications along the lines of ASP.NET MVC and the Web API. Here’s my tour of NancyFX, in which I focus on setup and the basics.

 

Why do I need another framework?

 

The development landscape is crowded with multiple options for everything, so deciding to use something (or not) can be confusing and tiresome. In the case of NancyFX, there are the Microsoft options — Web API and ASP.NET MVC — as well as open source offerings such as MonoRail and OpenRasta.

 

A big advantage of NancyFX is its simplicity, as it is relatively easy to get up and running and to start using it. The learning curve for the Microsoft options is not as straightforward, but that just may be me.

 

One of the biggest advantages of NancyFX is for those developers targeting a non-Windows platform via Mono. NancyFX is easily set up on your preferred flavor of Linux.

 

I know there is a lot of information available on running ASP.NET MVC and Web API on Linux via Mono, and I have not worked through the setup for each, but NancyFX seems like a simpler solution. This does not mean you cannot use the model-view-controller MVC within Nancy, as you can use whatever pattern that meets your need. Let’s take a closer look at setting up NancyFX.

 

Up and running

 

The installation and setup of NancyFX is straightforward: simply add the package to your application, and you are ready to go. For this article, I will focus on the Windows platform, with Mono covered in a future installment. I am using VisualStudio 2013. The example will utilize NancyFX for ASP.NET endpoints.

 

We begin with the creation of an empty ASP.NET web application as shown in Figure A and Figure B. You may notice that I am using Microsoft Azure to host the test site via the checkbox in Figure B. Once the empty site is created, NancyFX can be added to it via NuGet. Figure C shows the installation of the NancyFX packages. I install the base NancyFX features with the package called Nancy. In addition, the package for ASP.NET hosting via Nancy is installed (Nancy.Hosting.Aspnet).

 

The Visual Studio NuGet Package Manager may be used as well — its menu selection is shown in Figure D. The results of a search for all available Nancy-related packages is displayed in Figure E; notice there are other packages for hosting NancyFX solutions via OWIN, WCF, and others. Figure F shows the Package Visualizer (menu option in Figure D) open to display my installed packages — it provides a quick view of the hierarchy, as the ASP.NET hosting package relies on the base Nancy package.

 

Figure A

 

nancyfxfiga073114.jpg

Create a new ASP.NET web application.

 

Figure B

 

nancyfxfigb073114.jpg

The new ASP.NET web application will be empty.

 

Figure C

 

nancyfxfigc073114.jpg

Install Nancy packages via Visual Studio Package Manager Console.

 

Figure D

 

nancyfxfigd073114.jpg

NancyFX packages may be installed via Visual Studio NuGet Package Manager.

 

Figure E

 

nancyfxfige073114.jpg

The NancyFX packages that are currently available.

 

Figure F

 

nancyfxfigf073114.jpg

The Package Visualizer provides a visual hierarchy of installed packages.

 

With the new project created, we can use NancyFX features to serve content to requesting clients. Modules are the key ingredient of a Nancy-based application — all applications must have at least one. The module inherits from the NancyModule class. Routes and actions are defined within the module — they provide the behavior of your application. In addition to defining behaviors, the module provides access to the various properties of a request.

 

Figure G shows a simple route for handling a request to the nancyfxtest directory/route within the web application. The route or action is defined using the HTTP verb within the module method — in this case, Get is defined. The text in the return statement will be displayed when the request is processed. This application is hosted on Microsoft Azure, so I choose to publish it once it successfully compiles — Figure H shows the Publish option selected in the web application’s context menu. When the Publish option is used with Microsoft Azure, the window in Figure I is displayed to specify connection options. Once the site is pushed to Microsoft Azure, the results of requesting the route is displayed in Figure J.

 

Figure G

 

nancyfxfigg073114.jpg

A simple module for handling a basic route.

 

Figure H

 

nancyfxfigh073114.jpg

Using the Publish option to push the site to Microsoft Azure.

 

Figure I

 

nancyfxfigi073114.jpg

Specifying the options for connecting to Microsoft Azure.

 

Figure J

 

nancyfxfigj073114.jpg

The results of the request shown in Internet Explorer.

 

Figure J shows a basic route utilized; you can define more routes as well as a base route for the site or directory. The base route would be the default displayed for a request. The following block of code defines a base route for the root of our web application (forward slash signals the root).

 

using System.Web;

using Nancy;

namespace NancyFXExample  {

public class BaseModule : NancyModule   {

public BaseModule() : base(“/”)   {

Get[“/”] = _ =>   {

return “Base Module”;

};  }  }  }

 

This is just the tip of the iceberg with what NancyFX offers; the online documentation provides a wealth of information to go further with it — in particular, the section on defining routes and actions is great.

 

There are freely available templates that provide a starting point for building NancyFX-based applications. Figure K shows these templates viewed via the Extensions and Updates (available via Tools menu) window in Visual Studio.

 

Figure K

 

nancyfxfigk073114.jpg

NancyFX templates available for Visual Studio.

 

Plenty of options

 

The proliferation of open source options and Microsoft offerings provides avenues for building and deploying ASP.NET applications. While many developers choose to stick with Microsoft technologies because they feel Microsoft will never go away, others go with non-Microsoft options like NancyFX.

 

NancyFX provides a simple, straightforward approach to building and deploying web applications. I found it easier to get going from scratch, but no two developers are alike. NancyFX is fully extendible, so you are not restricted in how it is used. Keep NancyFX in mind for future projects, as it may meet the need.

Share the news now

Source : techrepublic.com