Skip to content

Media Type Formatters

pedroreys edited this page Apr 3, 2012 · 11 revisions

Media Type Formatters are responsible of deserializing the HttpRequestMessage body into a object that will be used as an input in a controller action. It is also responsible for serialize the object returned in the HttpResponseMessage according to the MIME type set in the Accept Header.

ASP.NET Web API ships with two media type formatters: XmlMediaTypeFormatter and JsonMediaTypeFormatter.

The WebApiContrib contains the following media type formatters:

ServiceStackTextFormatter

This formatter handles application/json MIME type. It uses ServiceStack.Text for serialization.

By default, ServiceStackTextJsonMediaTypeFormatter uses ISO8601 date format. To change it to use a different date format supported by ServiceStack.Text, provide the JsonDateHandler to be used during the serialization/deserialization process as an argument for the formatter constructor.

var formatter = new ServiceStackTextFormatter(JsonDateHandler.TimestampOffset);

JavaScriptSerializerFormatter

Placeholder for formatter description

JsonNetFormatter

Placeholder for formatter description

ProtoBufFormatter

Placeholder for formatter description


Registering a media type formatter to be used by ASP.NET Web API

To register a formatter to be used in ASP.NET Web API, add it to the Formatters collection in the GlobalConfiguration object.

protected void Application_Start()
{
    GlobalConfiguration.Configuration.Formatters.Add(new ServiceStackTextFormatter());

    // other registrations (routes, areas, global filters, etc)
}