Book Image

Instant Nancy Web Development

By : Christian Horsdal
Book Image

Instant Nancy Web Development

By: Christian Horsdal

Overview of this book

Nancy is a C# web framework which aims to provide you ,the application developer, with an easy path to follow, where things will naturally fall into place and work. Nancy is a powerful, flexible, and lightweight web framework that allows you to get on with your job. Instant Nancy Web Development will give Readers practical introduction to building, testing, and deploying web applications with Nancy. You will learn how to take full advantage of Nancy to build clean application code, and will see how this code lends itself nicely to test driven development. You will also learn how to hook into Nancy to easily extend the framework. Instant Nancy Web Development offers you an in-depth exploration of all the major features of the Nancy web framework, from basic routing to deployment in the Cloud, and from model binding to automated tests. You will learn how to build web applications with Nancy and explore how to build web sites using Razor views. Next, you will learn how to build web based APIs suitable for JavaScript clients, mobile clients, and even desktop applications. In fact, you will learn how to easily combine the two into one. Finally, you will learn how to leverage Nancy to write clean and maintainable web applications quickly.
Table of Contents (7 chapters)

Building and running your first Nancy application (Simple)


In this recipe, you will write your very first Nancy application. It will not do much, but you will see just how easy it is to get started with Nancy.

Getting ready

Before diving in and building your first Nancy application, there are a couple of things you will need. First and foremost, I'll assume that for this recipe and the rest of the book, you have Visual Studio 2012 Express for Web or a higher version installed, and you have the NuGet Package Manager extension installed and up-to-date. If not, go to http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-for-web#product-express-web, download and install Visual Studio, and then go to Tools | Extension and Updates... dialog and make sure NuGet Package Manager is up-to-date.

How to do it...

Going from zero to hello world with Nancy is as simple as following these steps:

  1. Open Visual Studio.

  2. Create a new empty ASP.NET project by navigating to File | New Project... and selecting the ASP.NET Empty Web Application template under Visual C# | Web. Let's call the project HelloNancy.

  3. Open Package Manager Console. At the prompt, type the following line of code:

    PM> Install-Package Nancy.Hosting.Aspnet

    The previous line of code will produce an output similar to this:

    Attempting to resolve dependency 'Nancy (≥ 0.17.1)'.
    Installing 'Nancy 0.17.1'.
    Successfully installed 'Nancy 0.17.1'.
    Installing 'Nancy.Hosting.Aspnet 0.17.1'.
    Successfully installed 'Nancy.Hosting.Aspnet 0.17.1'.
    Adding 'Nancy 0.17.1' to HelloNancy.
    Successfully added 'Nancy 0.17.1' to HelloNancy.
    Adding 'Nancy.Hosting.Aspnet 0.17.1' to HelloNancy.
    Successfully added 'Nancy.Hosting.Aspnet 0.17.1' to HelloNancy.
  4. Add a new C# file at the root of the project. Call it HelloModule and put the following code in it:

    namespace HelloNancy
    {
      using Nancy;
    
      public class HelloModule : NancyModule
      {
        public HelloModule()
        {
          Get["/"] = _ => "Hello Nancy World";
        }
      }
    }
  5. Press F5 and marvel at the fact that you've just created your first Nancy application. When you run this application, you should see something rather close to this:

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

How it works...

Let's take a quick look at what happened in the previous section. The interesting parts are steps 3 and 4.

In step 3, you installed the Nancy.Hosting.Aspnet package and the Nancy NuGet packages into the newly created HelloNancy project. This will download the Nancy assembly and the Nancy.Hosting.Aspnet assembly from the NuGet gallery, and add references to both in the HelloNancy project. The Nancy assembly is the Nancy framework, and the Nancy.Hosting.Aspnet assembly is an adapter for running the Nancy application on top of ASP.NET. In Nancy terms, this is called hosting Nancy on ASP.NET. Hosting is the subject of the Separating application and hosting (Advanced) recipe.

In step 4, you created a NancyModule class. Nancy modules are at the heart of Nancy applications. They are used to define which routes the application accepts and how it handles each one. For instance, the NancyModule class in step 4 defines one route and a handler for that route. This is done in the following line:

      Get["/"] = _ => "Hello Nancy World";

This tells Nancy that an HTTP GET request to the route "/" should be handled by the lambda expression on the right-hand side of the = sign. In other words, each time Nancy receives an HTTP GET "/", it calls the following line of code:

_ => "Hello Nancy World";

In the previous line of code, the lambda expression takes one parameter called _. It doesn't use the parameter for anything but simply returns the Hello Nancy World string back to Nancy. Since nothing went wrong, Nancy will create an HTTP 200 OK response with Hello Nancy World in the body and return it as the response to HTTP GET "/".

On a side note, I like to adopt the convention that whenever a parameter in a lambda is not used in the body of the lambda, I call it _. I like this because visually it makes the parameter almost disappear, so I instantly know that it is not used. On the other hand, if it is used, I will always give it a name. I will be using _ throughout this book for unused parameters.

On start-up, Nancy will find all Nancy modules in all loaded assemblies and instantiate each one. In the case of the module from step 4 in the previous section, the constructor containing the route definition discussed previously is run.

There's more...

This was just a very quick peek into how Nancy applications are written. Through the rest of the book, we will see many more features of Nancy, which will put you in a position to write real Nancy applications and not just Hello World.

Also, you should be aware of the Nancy documentation, which can be found at https://github.com/NancyFx/Nancy/wiki/Documentation, and the Nancy demos along with the Nancy source code at https://github.com/NancyFx/Nancy/tree/master/src.