Web API Versioning (using Header) – Part 2


In this post, I am going to discuss Web API versioning using custom Header. We are already discussed versioning using URI i.e.  namespace based versioning.

How versioning through Header?

To perform versioning through HTTP Header, we set a Header in request to differentiate API resources and on the server we have to check the Header and execute the controller that is specified by Header value.

Continue reading


Web API Versioning (using NameSpaceControllerSelector) – Part 1



In this article, I am going to discuss Versioning of Web API, its needs, approaches used for versioning and how we can implement that.

Why we need Versioning?

API versioning is very much required if we are exposing our services for the public, so it need to be our API should be forward and backward compatibility. By versioning, API  don’t affect the existing functionality and break user application which depends on our API.

How we are going to do this?

In the Web API default routing implementation map routes to the controller by ControllerClass name. Internally Web API select controller, for route, through DefaultControllerSelector which implement IHttpControllerSelector.SelectController. Using default controller selector we can’t really version web API, but we can implement our custom selector to select right version of our controller. We can easily create a custom HTTP controller selector by implementing IHttpControllerSelector.

Continue reading

What HTTP Status Codes Should We Use In Web API?



Every request we issue to the server, the server parses it and build the response to return the result to the client. As Web API is REST service, every response is represented with a state, a HTTP status should be semantically used to inform the client with result.

There are five types of HTTP status codes:

  1. Informational (1xx)
  2. Success (2xx)
  3. Redirection (3xx)
  4. Client errors (4xx)
  5. Server errors (5xx)

Every Group has its own details. For e.g., if during request all thing goes well, after GET request the status code of the response is 200 OK , but after a POST request is 201 CREATED.

Continue reading

Get Image response in Web API (Content Negotiation)

Content negotiation is an important part of REST API. It makes it possible to reveal a resource in various formats and allow the client choose which is best. A client can ask for the resource in a specific format using the Accept header. With this, the server serve the request to match the specific format and if the format is not supported, it returns a 406 Not Acceptable.

We saw examples in this article(Building a Simple Web API) that all the responses are JSON, but we can also get XML format too.

Continue reading

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

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

Building a Simple Web API

In this article we are going to create a simple Web API, that basically exposes an in-memory List<Student> over HTTP, for a client application to play with the list members.

Although this article would have relatively limited applicability to a practical web API implementation, it is a foundation toward understanding how to use the ASP.NET Web API framework to build your web API.

Step 1

Run Visual Studio and create a new ASP.NET Web Application. Give the project a name of StudentAPI. Select the ‘Web API’ template which will automatically select MVC and Web API Checkboxes. For now you can leave Authentication method use for API by default it is “No Authentication”.

Continue reading

Basics of Web API and REST

ASP.NET Web API is a framework built by Microsoft, which makes it easy to build HTTP services (REST) that reach a broad range of clients, including browsers and mobile devices. With Web API, if the client is requesting the data to be returned as JSON or XML, the Web API deals with the request type and returns the data appropriately based on the media type.

Web API is an ideal platform for building pure REST services where the request and response happens with HTTP protocol. The client can make a GET, PUT, POST, and DELETE request and get the response appropriately.

Before we go deep in Web API, let’s understand the concept of REST. Continue reading