The Mediatypeformatter provides the HTTP request body data with the. NET-type seamless conversions.
What is mediatype
Media type refers to the Content-type in the HTTP header, which defines the format of the data in the HTTP body. Media type is also used for the Accept header in HTTP Requestheader, indicating the format of the body of the response that the request expects to receive.
You can use the standard media type, such as Application/json, Application/xml. You can also define your own media type.
What is Mediatypeformatter
The mediatypeformatter is used to convert between HTTP body and. Net.
All mediatypeformatter inherit from the abstract class Mediatypeformatter
Public Abstract classmediatypeformatter{
//Properties PublicCollection<mediatypeheadervalue> Supportedmediatypes {Get;Private Set; } PublicCollection<encoding> Supportedencodings {Get;Private Set; } PublicCollection<mediatypemapping> Mediatypemappings {Get;Private Set; } //Methods Public Virtualtask<Object>readfromstreamasync (type type, stream stream, Httpcontentheaders contentheaders, Iformatterlogger Formatterlogger ) { //To is overriden by base class } Public VirtualTask writetostreamasync (Type type,Objectvalue, Stream stream, Httpcontentheaders contentheaders, TransportContext transportcontext) { //To is overriden by base class } Public Abstract BOOLcanreadtype (type type); Public Abstract BOOLcanwritetype (type type);}
To define your own mediatypeformatter, you only need to implement the above abstract classes.
Here's a detailed example.
Http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters
How to use custom formatter
To add a media type formatter to the Web API Pipeline,
Public Static void configureapis (httpconfiguration config) { config. Formatters.add (new productcsvformatter ()); }
When will the Web API framework use Mediatypeformatter
As we said earlier, media type is primarily used for the Content-type in the HTTP Request/response header, and for the accept in the HTTP Request header.
HTTP Request/response Header Content-type
When the Web API receives an HTTP request request and needs to read the body information (for example, using the Frombody property), the Web API checks the type of Content-type and then uses the registered formatter to deserialize.
The Accept in the HTTP Request header
Suppose we have a product class
[DataContract (Name ="Product", Namespace ="http://www.azure.com")] Public classProduct {[DataMember] Public intId {Get;Set; } [DataMember] Public stringName {Get;Set; } [DataMember] Public stringCategory {Get;Set; } [DataMember] Public decimalPrice {Get;Set; } }
and a controller for a Web API
Public httpresponsemessage Get () { varnew1"Gizmo" " Widgets " 1.99M }; return request.createresponse (Httpstatuscode.ok, product);}
The controller returns a Httpresponsemessage object that contains a product.
At this point, the Web API examines the contents of the accept in the header of the request, and then calls the corresponding formatter to go to the serial number product object.
We can use Fiddler to send the following request
user-Agent:FiddlerHost:localhost:9664accept:application/xml
So the response we get is
http/1.1 $Okcache-control:no-Cachepragma:no-cachecontent-type:application/xml; charset=utf-8Expires:-1Server:microsoft-iis/8.0X-aspnet-version:4.0.30319X-powered-By:ASP.NETDate:Sat, -Oct the ,: *: +gmtcontent-length:187<product xmlns:i="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.azure.com"><Category>Widgets</Category><Id>1</Id><Name>Gizmo</Name><Price>1.99</Price></Product>
Note the Content-type, as well as the body format.
If we send the following request
user-Agent:FiddlerHost:localhost:9664accept:application/json
The response we get is
http/1.1 $Okcache-control:no-Cachepragma:no-cachecontent-type:application/json; charset=utf-8Expires:-1Server:microsoft-iis/8.0X-aspnet-version:4.0.30319X-sourcefiles: =? utf-8? B? Yzpcdxnlcnnczwr3yw5nxgrvy3vtzw50c1x2axn1ywwgc3r1zglvidiwmtncuhjvamvjdhncv2viqxbwvgvzdfxxzwjbchbuzxn0xgfwavx2ywx1zxm =?=X-powered-By:ASP.NETDate:Sat, -Oct the the: A: $gmtcontent-length: ${"Id":1,"Name":"Gizmo","Category":"Widgets"," Price":1.99}
The Web API uses Xmlmediatypeformatter to process XML media type. Xmlmediatypeformatter is serialized using DataContractSerializer by default.
Use Jsonmediatypeformatter to process JSON media type. Jsonmediatypeformatter is serialized using Json.NET by default.
ASP. NET Web API Mediatypeformatter