How MediaTypeFormatter and MediaTypeMapping Are Associated With Each Other in Web Api

The Default Content Negotiation Algorithm is shipped as part of ASP.NET MVC4 WebAPI. The WebApi provides many nice features with which you can build Restful services. Conneg provides you MediaTypeFormatter and MediaTypeMapping.

If a Request message is sent with an Accept header then the Conneg algorithm will use the Accept header to decide about the Response media type and the MediaTypeFormatter to write. Here the client is asking for a response in a specific format.

If a Request message is sent with no Accept header and with a Content-Type header (let's say when you are using POST to post content to the server), the Conneg algorithm will use the Content-Type header to decide about the Response media type and the MediaTypeFormatter to write.

Here the Conneg algorithm uses its best approach to evaluate the format the client could understand. Since here the client was able to post content in a specific format like application/text, the Conneg algorithm decides that the Client could probably understand the Response also in the same format application/text.

There are the following four out-of-box MediaTypeFormatters that takes care of all these requests:

  1. JsonMediaTypeFormatter
  2. XmlMediaTypeFormatter
  3. FormUrlEncodedMediaTypeFormatter
  4. JQueryMvcFormUrlEncodedFormatter

There is one more way by which the Conneg algorithm decides about the Response's media type and formatter. It is called MediaTypeMapping. Every MediaTypeFormatter has a collection of these mappings in it.

By default, ASP.NET MVC4 Web API ships with the following 4 built-in media type mappings:

  1. QueryStringMapping
  2. RequestHeaderMapping
  3. UriPathExtensionMapping
  4. MediaRangeMapping

MediaTypeMapping allows you to write custom logic that fulfills the request. Fullfill the request means that a MediaTypeMapping has some value that exists in the request sent by the client that matches (like some value in the header). If it matches the request then it informs Conneg that a MediaTypeFormatter with the mediaType is ready to write and response.MediaTypeMapping provides full power to you to inspect the incoming request and make a decision of whether to participate in a response or not.

This is how both partcipate. In a subsequent article I'll explore MediaTypeFormatter.

What is the Precedence order of incoming request in WebApi handled by Conneg algo?

A definite question occurs when you work with the WebApi of how Conneg handles the request each time when multiple formatters match the request .There are certain ways like the Request Accept header, Content-Type header, MediaTypeMapping and so on that are considered by the Conneg to writing the response The Conneg algorithm must choose only one formatter in the end.

The Default Conneg algorithm has the following precedence order to select the final formatter:

  1. Formatter match based on Media Type Mapping.
  2. Formatter match based on Request Accept header's media type.
  3. Formatter match based on Request Content-Type header's media type.
  4. Formatter match based on if it can serialize the response data's Type.
  5. You can also use HttpResponseMessage to send the response despite of the Conneg algorithm overwrite it irrespective of the Request's Accept header media type.

Up Next
    Ebook Download
    View all
    Learn
    View all