Routing in Web API

In article we will see how ASP.NET Web API routes HTTP requests to controllers. Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP method, not the URI path, to select the action. We can also use MVC-style routing in Web API. A related difference is that Web API performs content type negotiation in response to incoming requests, and attempts to return the proper content type.

Web API route Configuration file

In a standard Web API project template, route configuration is called from  RouteConfig class, and a WebApiConfig class, both in the App_Start folder.

Continue reading

Advertisements

Web API : Configuration over Convention

In ASP.NET Web API, the default convention is to give the action method the same name as the HTTP verb or to name the method so that it starts with the HTTP method. In this article, we will override this default behaviour. 

For e.g., in the previous article we use method name GET to handle HTTP GET. So by convention action method can be GetStudents, GetAllStudent or GetStudentById.

By the same way, the action methods of POST, PUT, and DELETE will respectively handle HTTP POST, PUT, and DELETE.  To override this convention, we can use the AcceptVerbs attribute, or use  HttpGet, HttpPost, HttpPut, HttpDelete, and so on. Continue reading