Sunday 9 December 2012

NancyFx

What is NancyFx?


Nancy is a lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono. The goal of the framework is to stay out of the way as much as possible and provide a super-duper-happy-path to all interactions.


Nancy is designed to handle DELETEGETHEADOPTIONSPOSTPUT and PATCH requests and provides a simple, elegant, Domain Specific Language (DSL) for returning a response with just a couple of keystrokes, leaving you with more time to focus on the important bits.. your code and your application.

How to Get NancyFx in Visual Studio?


Finding a Package

From the Tools menu, select Library Package Manager and then click Package Manager Console.
Package Manager Console in menu
The Package Manager Console window is displayed.
Package Manager Console
The two drop-down lists set default values that let you omit parameters from the commands you enter in the window:
  • In the Package source list, select the default source (NuGet package feed) that you want your commands to use. Typically you will leave this as its default value ofNuGet official package source. For more information about alternative feeds, see Hosting Your Own NuGet Feeds.
  • In the Default project list, select the default project that you want your commands to work with. (The default value will be the first project in the solution, not necessarily the one you have selected in Solution Explorer when you open the window.)
When you enter commands, you can override these defaults. In the Package Manager Console window, enter Get-Package -ListAvailale at the prompt to see a list of all packages that are available in the selected package source.
Get-Package -ListAvailable command
For the default package source, that command is going to list thousands of packages. It makes better sense to specify a filter.
For example, to find the logging package ELMAH, enter Get-Package -ListAvailable -Filter elmah (the name of the package) or Get-Package -Filter Logging -ListAvailable (a keyword in the package description).
Get-Package command with filter
For more options that you can specify with the Get-Package command, enter Get-Help Get-Package, or see Package Manager Console Powershell Reference.

Installing a Package

After you have found a package that you want to install, use the Install-Package command with the name of the package. For example, enter the command Install-Package Nancy




Creating your first Nancy application


  1. Create a new ASP.NET Empty Web Application
  2. Grab the ASP.NET host for Nancy install-package Nancy.Hosting.Aspnet (this will also install the Nancy nuget and update theweb.config file so that Nancy is made the handler of any request)
  3. Add a Nancy module, which is a standard C# class, and define a route handler for the root URL of the web application, by adding a small amount of code to the constructor:
  4. Compile and run to see the result!
The HelloModule.cs code
public class HelloModule : NancyModule
{
    public HelloModule()
    {
        Get["/"] = parameters => "Hello World";
    }
}

0 comments:

Post a Comment

 
Copyright (c) 2010 Asp.net My life and Powered by Blogger.