Permalink
Fetching contributors…
Cannot retrieve contributors at this time
92 lines (54 sloc) 4.9 KB
uid title author description ms.author ms.date ms.assetid msc.legacyurl msc.type
web-api/overview/formats-and-model-binding/media-formatters
Media Formatters in ASP.NET Web API 2 | Microsoft Docs
MikeWasson
riande
01/20/2014
4c56f64a-086a-44ce-99c2-4c69604cd7fd
/web-api/overview/formats-and-model-binding/media-formatters
authoredcontent

Media Formatters in ASP.NET Web API 2

by Mike Wasson

This tutorial shows how to support additional media formats in ASP.NET Web API.

Internet Media Types

A media type, also called a MIME type, identifies the format of a piece of data. In HTTP, media types describe the format of the message body. A media type consists of two strings, a type and a subtype. For example:

  • text/html
  • image/png
  • application/json

When an HTTP message contains an entity-body, the Content-Type header specifies the format of the message body. This tells the receiver how to parse the contents of the message body.

For example, if an HTTP response contains a PNG image, the response might have the following headers.

[!code-consoleMain]

When the client sends a request message, it can include an Accept header. The Accept header tells the server which media type(s) the client wants from the server. For example:

[!code-consoleMain]

This header tells the server that the client wants either HTML, XHTML, or XML.

The media type determines how Web API serializes and deserializes the HTTP message body. Web API has built-in support for XML, JSON, BSON, and form-urlencoded data, and you can support additional media types by writing a media formatter.

To create a media formatter, derive from one of these classes:

Deriving from BufferedMediaTypeFormatter is simpler, because there is no asynchronous code, but it also means the calling thread can block during I/O.

Example: Creating a CSV Media Formatter

The following example shows a media type formatter that can serialize a Product object to a comma-separated values (CSV) format. This example uses the Product type defined in the tutorial Creating a Web API that Supports CRUD Operations. Here is the definition of the Product object:

[!code-csharpMain]

To implement a CSV formatter, define a class that derives from BufferedMediaTypeFormater:

[!code-csharpMain]

In the constructor, add the media types that the formatter supports. In this example, the formatter supports a single media type, "text/csv":

[!code-csharpMain]

Override the CanWriteType method to indicate which types the formatter can serialize:

[!code-csharpMain]

In this example, the formatter can serialize single Product objects as well as collections of Product objects.

Similarly, override the CanReadType method to indicate which types the formatter can deserialize. In this example, the formatter does not support deserialization, so the method simply returns false.

[!code-csharpMain]

Finally, override the WriteToStream method. This method serializes a type by writing it to a stream. If your formatter supports deserialization, also override the ReadFromStream method.

[!code-csharpMain]

Adding a Media Formatter to the Web API Pipeline

To add a media type formatter to the Web API pipeline, use the Formatters property on the HttpConfiguration object.

[!code-csharpMain]

Character Encodings

Optionally, a media formatter can support multiple character encodings, such as UTF-8 or ISO 8859-1.

In the constructor, add one or more System.Text.Encoding types to the SupportedEncodings collection. Put the default encoding first.

[!code-csharpMain]

In the WriteToStream and ReadFromStream methods, call MediaTypeFormatter.SelectCharacterEncoding to select the preferred character encoding. This method matches the request headers against the list of supported encodings. Use the returned Encoding when you read or write from the stream:

[!code-csharpMain]