ASP.NET MVC is an ideal platform for building web applications but in certain scenarios we need to access the data instead.For example we may write an ajax method and call the Action method which may return JSON response.This approach works but if our need is only accessing the data and the business logic then instead of using MVC ,WebAPI framework is a better option.Unlike the MVC framework which is supposed to return the HTML response ,WebAPI framework returns the data ,this is what we require in many scenarios such as when making ajax requests to update page content.
ASP.NET WebAPI uses many of the same concepts as the MVC framework like routes,controllers.But WebAPI differs from the MVC in the following ways
New Namespaces
In ASP.NET WebAPI instead of classes and interfaces being defined in the System.Web and System.Web.Mvc namespaces following two namespaces are used
- System.Web.Http Provides WebAPI functionality
- System.Net.Http Provides objects for implementing the HTTP in application
No Dependency on System.Web
System.Web is a monolithic assembly which contains much of the functionality of ASP.NET.It is used both by MVC and WebFoms.Since WebAPI was released after MVC and WebForms ,it’s classes and other types are not dependent on System.Web assembly.ASP.NET have been moving towards a design where developers can just select the required services and not enforced to include all the functionality.
In the following ASP.NET WebAPI example we will be creating a simple controller which returns the data to the client.
1)Select File –>New –>Project option. In the new project dialog select ASP.NET MVC application item
2)In the Select a template dialog we select the WebAPI template and click OK.
If we look in the solution explorer ,most of project items are similar to the ones in a MVC project.
In the controller folder we can find a ValuesController class which has been automatically created. ValuesController class is a controller class.It differs from the MVC controller in the following ways.
- Instead of the Controller class it derives from the ApiController
- There are different methods in the class which corresponds to the HTTP methods POST,GET,PUT and DELETE.These methods are called based on the HTTP method used to make the request.
If we look in the WebApiConfig.cs file which contains the WebApiConfig class ,it defines a Register method.Register method defines the routes in the application.
public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
Unlike the MapRoute() method in MVC ,WebAPI adds the routes using the MapHttpRoute() method.
There is one important difference to understand between WebAPI and MVC.Instead of explicitly mapping action methods using the routes ,the methods are called depending upon the HTTP verb being used to access the controller.For example if the verb used in the request is GET then the WebAPI will look for a method starting with GET.This is an example of convention over configuration.
We can call the Get method in the controller class using the following ajax method.
For Get requests we need to pass the type as “GET”
$.ajax({ url: "http://localhost:portNumber/api/Values", type: "GET", success: function (data) { //success } });
If we need to call the POST method we need to pass the type as “POST” in the ajax method.
$.ajax({ url: 'http://localhost:portNumber/api/Values/', type: 'POST', data: JSON.stringify(data), contentType: "application/json;charset=utf-8", success: function (data) { //success }, error: function () { //error } });
Leave a Reply