Content negotiation means: When there are multiple content-type to choose from, select the most appropriate one to serialize and return to the client.
These properties are determined primarily by the Accept, Accept-charset, accept-encoding, accept-language in the request, but other properties are also viewed
For example, if the request contains "X-requested-with" (Ajax request), the JSON is used when no other accept is available.
How the Content negotiation works
First, pipeline obtains the icontentnegotiator service from the httpconfiguration object, and also from the httpconfiguration.formatters to get all media formatters. Next, Pipeline calls icontentnegotiatior.negotiate to pass in the value :
- The object to serialize
- All media formatters
- HTTP Request
Negotiate Returns a value of 2:
- Formatter of choice
- The media type to output
If there is no suitable formatter, theNegotiate method returns null,client will receive a 406 error (unreachable)
The following is an example of a controller using the content negotiation directly "
1 PublicHttpresponsemessage GetProduct (intID)2 {3 varProduct =NewProduct ()4{id = id, Name ="Gizmo", Category ="Widgets", Price =1.99M };5 6Icontentnegotiator negotiator = This. Configuration.Services.GetContentNegotiator ();7 8Contentnegotiationresult result =negotiator. Negotiate (9 typeof(Product), This. Request, This. Configuration.formatters);Ten if(Result = =NULL) One { A varResponse =Newhttpresponsemessage (httpstatuscode.notacceptable); - Throw Newhttpresponseexception (response)); - } the - return Newhttpresponsemessage () - { -Content =NewObjectcontent<product>( +Product//What we are serializing -Result. Formatter,//The Media formatter +Result. Mediatype.mediatype//The MIME type A ) at }; -}
View Code
The default content negotiator
The Defaultcontentnegotiator class is the default implementation of Icontentnegotiator , which uses the following criteria to select Formatter.
First, this type must be able to serialize, this step is verified by Mediatypeformatter.canwritetype to achieve
Second, content negotiator looks at each formatter and calculates the most formatter with the HTTP request. The principle of matching is as follows:
- The supportedmediatypes collection includes all supported media type, and content negotiator uses the Accept header to match this set.
Note that the Accept header can be a range. For example, "Text/plain" is a match for text/* or "/"
- The mediatypemappings collection contains a series of mediatypemappings objects that provide a match between the HTTP request and a media type. Such as
The custom HTTP header can be mapped to the specified media type
If a suitable match is not found according to the accept header, ontent negotiator attempts to match media type according to the request body. For example, if the request contains JSON data, content negotiator will use the JSON formatter.
If a suitable match is not found at this point, content negotiator uses the first formatter that can serialize the object.
When formatter is OK, content negotiator will look at this formatter supported supportedencodings and match the Accept-charset header to find the most appropriate Character encoding.
WEBAPI2 website Study record---Content negotiation