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,
Now, select Web API, as shown below.
Now go to ValueController,
- public class ValuesController: ApiController {
-
- public IEnumerable < string > Get() {
- return new string[] {
- "value1",
- "value2"
- };
- }
-
- public string Get(int id) {
- return "value";
- }
-
- public void Post([FromBody] string value) {}
-
- public void Put(int id, [FromBody] string value) {}
-
- public void Delete(int id) {}
- }
Now run the application and open fiddler,
- Go to Compose tab
- Add below xml format as accept header
Accept: application/xml
- Now click on Execute button and click on XML table in Response Body as shown below,
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
Now you can even specify multiple formatters at the same time using comma as separator
Now you can also specify quality formatter in accept
- Accept: application/xml;q=0.8
- Accept: application/json;q=0.5
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.
- var jsonp = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter);
- 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.
- config.Formatters.Remove(config.Formatters.XmlFormatter);
And if you want use only XML as default datatype then go into Register() of WebApiConfig
- config.Formatters.Remove(config.Formatters.JsonFormatter);
In the next article, we will be discussing custom method names and ways of calling WebAPI using jQuery.