ASP.NET Web API Media Type Formatter

Overview - In this article you will see,

  • What is MediaTypeFormatter 
  • How to return only JSON from ASP.NET Web API Service.
  • How TO RETURN xml from ASP.NET Web API Service.
  • How to return JSON instead of XML from ASP.NET Web API service when a request is made from the browser. 
  • How to return CSV formatted data from ASP.NET Web API
What is MediaTypeFormatter?

MediaTypeFormatter is an abstract class from which JsonMediaTypeFormatter and XmlMediaTypeFormatter classes inherits.
  
For example - In ASP.NET Web API we had got this JsonMediaTypeFormatter class so this is the class that deals with JSON data, if we issue a request to Asp.net Web API service with accept values application-value/json we get data formatted in JSON and it is in this class JsonMediaTypeFormatter who is doing for us. This class inherits from MediaTypeFormatter.

Similarly, XmlMediaTypeFormatter class deals with XML Data and this class also inherits from MediaTypeFormatter class. Let’s see our code in WebAPIController.cs

Formatter
Look at this property JsonFormatter if you go to definition its actually returning instance of type JsonMediaTypeFormatter as,
Formatter

So Right click on JsonMediaFormatter and go to definition you will see,

Formatter

JsonMediaTypeFormatter class is inheriting from BaseJsonMediaTypeFormatter . Now right click on that and go to definition you will see,
Formatter

BaseJsonMediaTypeFormatter is inheriting from MediaTypeFormatter. So Mediatypeformatter is inheriting from this abstract class.

Similarly, we will look for XMLFormatter we will modify that property in our controller class as,

Formatter
Now click on go to definition you will get,

Formatter

We had got an instance of XmlMediaTypeFormatter . Now again press F12 you will see,
Formatter

XmlMediaTypeFormatter inherits from MediaTypeFormatter .

Now if we want to create a custom Formatter now let’s suppose that is going to return data in CSV format in those case we will create a class which is going to inherit from MediaTypeFormatter and also implements its abstract members. We see that in a bit.

How to return only JSON from ASP.NET Web API service.

So let’s flip to visual studio in that comment those lines JSON serialize formatter and we will use config formatter to remove XML formatter completely as,

Formatter
Now whenever we are going to request the service is always going to format the data using JSON Formatter irrespective of the acceptheader value in the request.

You can use this in service when it only supports JSON data. So build the solution and run the app.

Formatter
As you notice response is JSON format as expected. Now let’s issue this request from fiddler,

Formatter

You will see the data in JSON irrespective of the fact that accept header value is XML.

Now we will see how to return only XML from ASP.NET Web API service.

To do that we will do just the opposite of that so let’s flip to our controller code instead of XML we will write JSON as,

Formatter
Now save the solution and run the app you will see.

Formatter
As you can see the data we got is in XML format. Now we will see the request in fiddler as,

Formatter

Irrespective of the accept header type we had got output in XML format instead of JSON. If we write accept header as JSON we will get output in XML only.

Now let’s see how to return JSON instead of XML from ASP.NET Web API Service when a request is made from the browser.

Now we will comment the code run the app and issue the request as,

Formatter
Run the app

Formatter
As you can see from the output by default any request is made we get data in XML format from the browser and that’s because the accept header value is in the request. Let’s inspect the request in fiddler.

Drag and drop request to the composer tab as below,

Formatter

As you can see the accept header value is text/html so for this accept header value the asp.net web api service is always going to return XML. But we want to do the opposite instead of XML it should return JSON.

If the request is made from the browser we want output to be in JSON and if the request is made from the fiddler specifying accept header as XML or JSON we will get the output corresponding to how the accept header is issued.

Flip to Visual studio and this line of code,


In this we had use SupportedMediaType attribute and in that we are passing text/html. So when the accept header value is text/html we want to use JSOn formatter to format the data. Now run the app,


Notice we got JSON data. Now let’s inspect this in fiddler,



When you go to fiddler and inspect that URL it will give you a message like above --  click on decode. Click on JSON tab,

Formatter

We had got JSON formatted data, but then click on the header and look for the content type header it’s still text/html,

Formatter

But we got data in JSON so this is a bit misleading but if we now go to compose tab and write application/xml and execute that as,

Formatter



We are going to get XML back and application value is content type application/xml.

But for JSON output we had got application type text/html we want as /JSON so we need to modify our code as,

Create a public class and we will create a customJson formatter and that class will inherit from JsomMediaTypeFormatter . This is inheriting from system.Net.http.Formatting just make sure you have that namespace included,

Formatter

So in this code we had used a constructor and in that we had added a supported mediatypes as text/html and we are overriding a method called as SetDefaultContentHeader in that we are setting content type as application/json . Now we will register that method in our register method as,

Formatter
Now run the solution,


We had got JSON output . Lets inspect this request in fiddler as,

In JSON tab we had got JSON data,

Formatter

Now we will click on Headers and see the content type as,

Formatter

As you can see the content type is application/json.

Now the last part is how to create custom CSV Mediatype Formatter

Follow this URL for CSV media type formatter.


Similar Articles