ASP.NET Web API is Framework for building for building HTTP services on top of the .NET Framework. It ships with ASP.NET MVC 4. You can install the ASP.NET MVC 4 from here. ASP.NET Web API is a integrated Framework resulted in joint effort from WCF and ASP.NET teams. WCF REST is now replaced by ASP.NET Web API. You can read the post to migrate your existing WCF Web API to ASP.NET Web API. This post briefly discuss about why we need it and how we can use it in ASP.NET and other projects. |
Why use ASP.NET Web API?
- Reach More Clients – By creating HTTP services by using client appropriate formats you making your application broadly available. Your services not just consumed by browsers but also client applications, mobile applications and other devices. In this way you are having interoperability with other applications directly by consuming your services
- ASP.NET Web API uses content negotiation which makes it easy to provide the right format for each client like XML, JSON or custom formats
- Scale with the Cloud – by using fully asynchronous task based service framework which provide light-weight hosting options including windows azure web or worker roles.
- Embrace HTTP HTTP is an application level protocol and is supported on clients. It really simplifies the communication with clients.
More about the ASP.NET Web API features can be read here
Create a new project in Visual Studio and select ASP.NET MVC 4 web application template
Web API is one of the new built-in project template which you can select as shown in the below dialogue
This creates a new controller named ValuesController in your ASP.NET MVC application which implements APIController has some simple methods for returning sample data like Get, Put , Post and Delete. These are mapped to HTTP standard verbs.
These are implemented using standard ASP.NET MVC routing behavior, You can find a route find a ASP.NET Web API
Run the application and see the output, you can not browse directly the output of Web API but you can see what it is doing by using developer tools
Using the IE developer tools capture the output by typing the URL as shown below
if you look at the body of the response it is returning the below output
the above output is directly maps to output of the Get method in ValuesController.
The self hosting capability of WCF is still possible with ASP.NET Web API, all the features that you see in ASP.NET MVC routing, action based controller are still work out side web application. You can see the same features in console application by self hosting the service
tell the application where to listen for the service then the above code is using full asp.net routing and working in self host console application.
Share this post : |