ASP.NET Web API 2 Media Type Formatter

ØIntroduction  _

As mentioned in the previous ASP.NET Web API 2 message processing pipeline article, in the declaration cycle of Web API , there is also an important part of the comparison, that is, the media type formatter, which is mainly used for processing Web API . Format processing of request and response data, such as JSON , XML processing programs, etc. are commonly used.

Ø  Hint: For simplicity, the "media type formatter" is called "formatter" below.

This article mainly covers the following points:

1.   What is a formatter

2.   Set the default formatter

3.   Determine the response data format ( JSON/XML ) according to the parameters

4.   JSON formatter related settings

5.   XML formatter related settings

 

1.   What is a formatter

1)   Formatter is some classes used to parse request and response data, these classes directly or indirectly inherit from the abstract class System.Net.Http.Formatting.MediaTypeFormatter . We can personalize the behavior of the formatter according to our needs, or rewrite the formatter (we will not cover this content in this article).

2)   When a request arrives, the message handler will find a matching formatter according to the C on tent-Type in the request header to parse the request data. For example: Content-Type:application/json is parsed using System.Net.Http.Formatting.JsonMediaTypeFormatter .

3) Similarly, the response data format finds a matching formatter according to the    Accept type in the request header , parses the response data, and then converts it into a byte response request. Of course Web API also supports URL parameters to map the required formatter.

 

2.   Set the default formatter

Web API includes the following four formatters by default. Among them, Json and XML formatters are more commonly used. If the request header Accept does not specify the expected type (that is, Accept:*/* ), Web API will follow the formatter collection. One of them to parse, that is , the formatter with index 0 . You can see that the default is the Json formatter.

clip_image002

 

1)   After figuring out the principle, it is easy to do, add the code to WebApiConfig.cs :

var formatter = config.Formatters.XmlFormatter;

config.Formatters.Remove(formatter);

config.Formatters.Insert(0, formatter);

 

2)   This defaults to the XML formatter and sends the request:

1.   URLGET /api/basic/getCustomers HTTP/1.1

2.   Request Accept: */*

3.   Response Content-Type: application/xml; charset=utf-8

4.   Response Text:

clip_image003

 

3.   Determine the response data format ( JSON/XML ) according to the parameters

1)   Add code to WebApiConfig.cs

config.Formatters.JsonFormatter.MediaTypeMappings.Add(new System.Net.Http.Formatting.QueryStringMapping("t", "json", "application/json"));

config.Formatters.XmlFormatter.MediaTypeMappings.Add(new System.Net.Http.Formatting.QueryStringMapping("t", "xml", "application/xml"));

 

2)   Send the request

1.   URLGET /api/basic/getCustomers?t=json HTTP/1.1

2.   Request Accept: */*

3.   Response Content-Type: application/json; charset=utf-8

4.   Response Text:

clip_image004

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325212624&siteId=291194637