ASP.NET Web API - Part Three

In Part 1 and Part 2 of this series, we discussed HTTP Verbs and their implementation. Now, let’s discuss Content Negotiation and Mediatype Formatters.

Content Negotiation

As per one of the standards of RESTFUL services, the client should be able to decide the format of response they need from servers like JSON, XML or any other type.

When a request is sent to the server, the request contains Accept Header.

Now, the format of the response can be specified into the content header; such as if the Accept Header is set to application.xml then the response will be sent in XML format by the server. And if it is set to application/json, then the response will be in JSON format.

Now, let’s understand by example.

File - New Project - Select Asp.Net Web Application and add the name of the project, as shown below,

ASP.NET

Now, select Web API, as shown below.

ASP.NET

Now go to ValueController,

  1. public class ValuesController: ApiController {  
  2.     // GET api/values  
  3.     public IEnumerable < string > Get() {  
  4.         return new string[] {  
  5.             "value1",  
  6.             "value2"  
  7.         };  
  8.     }  
  9.     // GET api/values/5  
  10.     public string Get(int id) {  
  11.         return "value";  
  12.     }  
  13.     // POST api/values  
  14.     public void Post([FromBody] string value) {}  
  15.     // PUT api/values/5  
  16.     public void Put(int id, [FromBody] string value) {}  
  17.     // DELETE api/values/5  
  18.     public void Delete(int id) {}  
  19. }  

Now run the application and open fiddler,

  • Go to Compose tab
  • Add below xml format as accept header

Accept: application/xml

ASP.NET

  • Now click on Execute button and click on XML table in Response Body as shown below,

    ASP.NET

Now in the above image, since we have specified the format in as XML in Header, we got it in XML format.

Now similarly for JSON also you need to specify Accept as Accept: application/xml as shown in below image

ASP.NET

Now you can even specify multiple formatters at the same time using comma as separator

ASP.NET

Now you can also specify quality formatter in accept

  • Accept: application/xml;q=0.8
  • Accept: application/json;q=0.5
ASP.NET

Default return type in Web API is JSON. If you do not specify anything in Accept Header, then it will be JSON.

MediaType Formatters

Now, one thing to remember is that these MediaType formatters are used by server both for request and response messages. When a client sends the request to the server, we set the content type header to the appropriate value to let the server know format of the data to be sent.

If the content type is JSON then server knows it has to deal with JSON data, so it uses jsonformatter to convert it into .NET type.

Similarly, when response needs to be sent, then it uses an appropriate converter to use the converted value.

We can change these MediaType fomrtters by configuring it into Register() of WebApiConfig.

  1. var jsonp = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter);  
  2. config.Formatters.Insert(0, jsonp);  

Now, MediaTypeFormatter is an absract class from which all formatter classes are derived.

As shown in the below example,

  • JsonMediaTypeFormatter : BaseJsonMediaTypeFormatter : MediaTypeFormatter
  • XmlMediaTypeFormatter : MediaTypeFormatter

If you want to use only JSON as default datatype then go into Register() of WebApiConfig.

  1. config.Formatters.Remove(config.Formatters.XmlFormatter);  

And if you want use only XML as default datatype then go into Register() of WebApiConfig

  1. config.Formatters.Remove(config.Formatters.JsonFormatter);  

In the next article, we will be discussing custom method names and ways of calling WebAPI using jQuery.


Similar Articles