Wednesday, 18 January 2017

,

Nancy API

 

1. What is Nancy?

Nancy is a lightweight, low-ceremony, framework for building HTTP based services on .Net
The main difference between Nancy and Web API is the routing approach. It uses lambdas to identify relative paths and arguments.
Nancy is a good example, as it brings the Ruby Sintra web framework to the .NET world.


2. Hosting Technique

There is two different approach is given for hosting Nancy API.
Self-Hosting   &  ASP.NET hosting. 

- Nancy API with Self hosting.

After creating API using nancy we have to provide the hosting environment over which it can run.
self hosting technique which host the whole API by itself, it's seems like a console app.

- Nancy API with ASP.NET hosting.

ASP.NET hosting is used when we want to make real use of our Nancy API.
It’s very simple to create and access different methods in Nancy API, let’s look by simple example, follow the below steps to create and host the Nancy API.
First, create new project with ‘ ASP.NET empty web Application’ as shown in bellow,



A new project is created which contain no items.

Now, Install NuGet package ‘Nancy’  and 'Nancy.Aspnet.Hosting '

Then create new class named ‘NancyHome’ and as use namespace Nancy as shown below,


The "NancyModule" class is referenced from the Nancy library and inherited into "MainModule". Like Controllers in MVC there are Modules in Nancy. A Module defines the behavior of the application. A single Module must be defined for a Nancy application, which is the starting point of an application. We can create any number of classes which can inherit Nancy Module.

Then,

As, you seen above image first we have to constructor with no parameter then create two methods for two different rout.
As shown in above methods, Inside Get Request we whatever we define that is set as the routing parameter for that particular request.

When you run this project, by default the first GET method having ‘[“/”]’ as  route parameter is called first.


Then if you change URL with ‘/test’ post fixed to the existing URL it’s called the GET method which have ‘[“/test”]’ as a rout parameter.


Note: When you make a call through the URL of browser it is considered as you are requesting for  ‘HTTPGET ‘method.
Here, I have created for only GET request similarly we can also create POST, PUT, and DELETE.


3. Enable CORS with Nancy.

Many time you come across with the term ‘CORS’ when you deal with API’s.

CORS : Cross Origin Request Resources 


here,For example API is hosted on www.contoso.com and your application is on www.example.com, so whenever you are requesting API on www.contoso.com from your application (www.example.com) is called cross origin request and we have to take care little more for this type request.

And for that we need to write some(as per below) more code which enable this type of cross origin request.


And for that we need to write some(as per above) more code which enable this type of cross origin request.

As shown in above image we need to add two namespace Nancy.Bootstrapper and Nancy.TinyIoc.

RequestStartup method make enable the cross origin request by adding response header with ‘Access-Control-Allow-Origin’ to ‘*’ and also ‘Access-Control-Allow-Methods’ to ‘Put, Post, Get, Delete’

Now you are able to create Nancy API and you can configure it for same domain as well as cross domain request.

Share:

0 comments:

Post a Comment