MediaTypeFormatters in WebApi

In this article I'll share my Media Type formatting in the WebApi.

From the ASP.NET Web API perspective, serialization is the process of translating a .NET Common Language Runtime (CLR) type into a format that can be transmitted over HTTP. The default format can be either JSON or XML

A media type formatter that is an object of type MediaTypeFormatter, performs the serialization in the ASP.NET Web API pipeline. Consider a simple action method handling GET in an ApiController:

Get

This method returns a CLR object of type Employee. In order for the data contained in this object to be returned to the client in the HTTP response message, the object must be serialized. The MediaTypeFormatter object in the ASP.NET Web API pipeline performs this serialization. It serializes the object returned by the action method into JSON or XML, that is then written into the response message body. The out-of-box media formatters that produce JSON and XML are respectively JsonMediaTypeFormatter and XmlMediaTypeFormatter, both deriving from MediaTypeFormatter. The process through which the MediaTypeFormatter is chosen is called content negotiation, also known as conneg.

A resource can have one or more representations. When you issue a GET to retrieve a resource, such as the employee with ID 12345, the response message contains the representation of the resource for example any value that specific action returns. The Web API indicates how the resource is represented in the response through the Content-Type response header. The Accept request header can be used by a client to indicate the set of preferred representations for the resource in the response.

By default the ASP.NET Web API framework supports two media or content types: JSON and XML.If you send a request with Accept: application/json, the response message will be JSON and Content-Type will be application/json. Similarly, if you send a request with Accept: application/xml, the response message will be XML. You can also specify a quality value indicating the relative preference. The range is 0–1, with 0 being unacceptable and 1 being the most preferred. The default value is 1. For example, if you send the request header Accept:

application/json; q=0.8, application/xml;q=0.9
 
Then the response message will be XML, because application/xml has a quality value of 0.9, that is higher than the quality value of 0.8 specified for application/json.

Now we use an example and list out the default MediaTypeFormatters.

Kindly also visit these links to learn more about the WebApi.

Use the following procedure to create a sample application.

Step 1: Let's first create a sample web application using an ASP.NET MVC 4 Web Application and name it as you choose; I used WebApiDemo as shown in the image below:

WebApiDemo

Step 2: Click "OK" and choose the Web API option from the templates shown in the wizard window.
 
Web API option

Step 3: You'll find the application structure as shown below at first sight.
 
application structure

Step 4: Right-click the Controllers folder in the Solution Explorer of Visual Studio. Select "Add" ➤"Controller" and provide a name of EmployeesController for the controller. Leave the option "Empty API Controller" selected in the Template dropdown and click "Add", as shown in the figure below. Notice that the generated controller class inherits from ApiController, a class that is part of the ASP.NET Web API framework. Kindly add the following code to the EmployeesController class.
 

public static IList<Employee> listEmp = new List<Employee>()

{

    new Employee()

    {

        ID =001, FirstName="Sachin", LastName="Kalia"

    },

    new Employee()

   {

       ID =002, FirstName="Dhnanjay" ,LastName="Kumar"

   },

   new Employee()

  {

      ID =003, FirstName="Ravish", LastName="Sindhwani"

  },

  new Employee()

  {

      ID =004, FirstName="Amit" ,LastName="Chaudhary"

  },

}; 

 
EmployeesController class 

Step 5: Right-click the Models folder in the Solution Explorer of Visual Studio. Select "Add" ➤ "Class" to add a new class with the name "Employee".

add a new class

After creating the Employee class, kindly add the following code into this class.
 

public class Employee

{

    public string ID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }
   
public string Department { get; set; }

}

Modify the Register method in WebApiConfig in the App_Start folder and add some code segment into this to list out the default MediaTypeFormatters as shown in following image:

code

Press F5 and run your application; it will show the following image:
 
run your application

Great, the WebApi is up and running.

Now look at the output window of your application, it gives you the details of each line that we've placed into the WebApiConfig.cs file.
 
output window

From the serialization point of view, the last two media type formatters can be ignored, since they cannot write any type. The first two, JsonMediaTypeFormatter and XmlMediaTypeFormatter, are the important ones. They are the media formatters that produce JSON and XML resource representations in the response

Conclusion

In this article we looked into default MediaTypeFormatters of WebApi, that are very much essential from the user's perspective.