ASP.Net Web API - Media Type Formatter

This is another series of Web API blog. In the previous blog, we have seen about Web API Security, Routing in Web API.
This blog will help you to understand Media Type Formatter in ASP.Net Web API. Earlier I have already written about "How do I get ASP.Net Web API to return JSON"

What is Media Type?

A media type also called a MIME type, which identifies the format of a piece of data sends over HTTP protocol. A media type consists of two strings, a type, and a subtype. For example:

text/html
application/json

When an HTTP Post request made, header specifies the format of the message body. This helps the receiver to help how to parse the content in the message body.

For example, if an HTTP response contains text format data then the response header will look like this.

HTTP/1.1 200 OK
Content-Length: 55358
Content-Type: text/html


Media type determines how Web API serializes and deserializes the HTTP message body. As we know Web API supports XML, JSON, additionally we can use other media type in Web API by using below 2 classes.

MediaTypeFormatter - Base class to handle serializing and deserializing strongly-typed objects.
BefferedMediaTypeFormatter - Represents a helper class to allow asynchronous formatter on top of the asynchronous formatter infrastructure.

Let's see the properties and methods of these 2 classes.

Media Type Formatter:


Property
Property Name
Description
MaxHttpCollectionKeys
Gets or sets the maximum number of keys stored in a T: System.Collections.Specialized.NameValueCollection.
SupportedEncodings
Gets the mutable collection of character encodings supported bythis MediaTypeFormatter.
SupportedMediaTypes
Gets the mutable collection of media types supported by this MediaTypeFormatter.
MediaTypeMappings
Gets the mutable collection of MediaTypeMapping objects that match HTTP requests to media types.

Method

Method Name
Description
CanReadType(Type)
Queries whether this MediaTypeFormatter can deserialize an object of the specified type.
CanWriteType(Type)
Queries whether this MediaTypeFormatter can serialize an object of the specified type.
GetDefaultValueForType(Type)
Gets the default value for the specified type.


BefferedMediaTypeFormatter:


Property


Property Name
Description
BufferSize
Gets or sets the suggested size of buffer to use with streams in bytes.
MediaTypeMappings
Gets the mutable collection of MediaTypeMapping objects that match HTTP requests to media types.
SupportedEncodings
Gets the mutable collection of character encodings supported bythis MediaTypeFormatter.
SupportedMediaTypes
Gets the mutable collection of media types supported bythis MediaTypeFormatter..

Method 

Method Name
Description
CanReadType(Type)
Queries whether this MediaTypeFormatter can deserializean object of the specified type.
CanWriteType(Type)
Queries whether this MediaTypeFormatter can serializean object of the specified type.
WriteToStream(Type, Object, Stream, HttpContent)
Writes synchronously to the buffered stream.
WriteToStream(Type, Object, Stream, HttpContent, CancellationToken)
Writes synchronously to the buffered stream.




Prev Blog-  ASP.Net Web API Security
Next Blog- ASP.Net Web API CRUD operation (Upcoming blog)


Keep following  - SharePointCage.Net

Comments

Popular