Quantcast
Channel: Alagesan Palani » ASP.NET Features
Viewing all articles
Browse latest Browse all 2

Introductory demo on ASP.NET WebAPI

0
0

Welcome back, this is the second article in the series of articles on the ground breaking Microsoft REST based SOA technology framework, ASP.NET Web API.  I am going to show you some hands on demo on Implementing ASP.NET WebAPI using Microsoft Visual Studio 2012.

My previous article in this series on Introduction to ASP.NET WebAPI can be found in this link: http://alagesann.com/2012/10/31/a-detailed-introduction-to-asp-net-web-api/

If you have knowledge on ASP.NET MVC already, you probably know the most part of implementing a WebAPI because ASP.NET Web API structured, Implemented and routed almost like an ASP.NET MVC application, though there are some significant differences which we will explore shortly. So I assume here that you have atleast a little knowledge on the ASP.NET MVC  technology.

I am using Visual Studio 2012 ultimate for this demo.

You can create a simple Web API project just in 2 steps:

  1.  Create a new project by using ASP.NET MVC 4 Web Application project template.

Give a name for the project; I am using the name as “BookStore” and choose a location to store your project. Click OK.

2.  In the following window, choose the Web API to make the project ready for implementing web api. You can choose any template in order to build a web api project, but I am using Web API template so it will give me a sample Api controller automatically. Leave Razor view engine selected and since I am not using a unit test for now for simplicity, leave that check box unchecked.

Now you are landed on the visual studio working window where you can see an initial window like this:

Important points to note here with respect to WebApi are:

  1. System.Web.Http dll is referenced automatically for you to build the web Api functionality.
  2. WebApi is installed by default as a nuget package for the projects you create in VS 2012. You can check that by going through “Manage Nuget  Packages” option from the  context window that appears when you right click on the project. “ Microsoft ASP.NET WebApi “ is installed as shown in the following Nuget Packages window: 
  3. A Default WebApi controller named “ValuesController” is added to the “Controllers” folder and this controller is extended from “ApiController” (will explore why is that shortly). Most importantly this controller has methods “Get, Get by Id, Post, Put and Delete” implemented.
  4. A WebApi route configuration file “WebApiConfig.cs” is added to the “App_Start” folder.

Okay, enough of analyzing some basic setups, lets browse the project and try to navigate to the default Apis created by Visual studio.

I am using google chrome, so let’s use it and play with the urls. When I try the url: /Values/Get/
to invoke the “Get” action of the ValuesController, it returns a 404 error. It should supposed to work as this is how it’s been working in ASP.NET MVC, but now it disappoints.
So let’s check what is there in the routing configuration for this WebApi project. Routing is defined in the WebApiConfig.cs as:

config.Routes.MapHttpRoute(
name: “DefaultApi”,
routeTemplate: “api/{controller}/{id}”,
defaults: new { id = RouteParameter.Optional }
);

So it turns out that, WebApi slightly works different from ASP.NET WebApi from routing perspective. WebApi routing has the word “api” in the route before the controller name. So the url must be

/api/Values/

Also note from the route template that there is no action name given right after the controller name. So How the appropriate action is going to be invoked when we hit the url. That’s an another aspect that ASP.NET MVC routing different from WebApi.

Http verbs like Get, Post, Put and Delete that are used to invoke the url are directly mapped to the   controller actions.

So I have to hit the url “/api/Values” it’s going to invoke the Get action from the ValuesController because it’s the “Get” http verb that’s used internally by the browser when I hit the url and I see the result as follows:

Cool, our Api now worked and it returned the response in the form of xml. But, wait. We just return the array of strings from the Get action and how it returns them as xml? It’s all the magic of Content-negotiation feature of WebApi framework.

WebApi supports many content forms starting from XML, JSON to ATOM and best of all it has the capability to be extended programmatically.

Okay, XML is fine. Now, how to retrieve the values in the form of JSON? This needs a little more capability on invoking the urls than the browser itself. So I am going to use Fiddler tool for that which you can download and install from the url:  http://www.fiddler2.com/fiddler2/version.asp and there are lot of videos and information on the fiddler for information on how to use it. So let’s directly use fiddler and get the JSON representation of the response.

My request header looks like this:

Host: localhost:8425
User-Agent: Fiddler
Content-Type: application/json; charset=utf-8

And hit Execute.

we get the response as:

Note the Json response of the response.

Summary:

We explored how to create a sample ASP.NET WebAPI project and its structure and routing mechanisms. We also tried to execute the apis using browser and fiddler. In the next article, we will explore how to implement a real service project, BookStore Service, using ASP.NET Web Api.



Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images