Asp. Net Web API 2 14th course -- Content Negotiation (Content Negotiation)

Source: Internet
Author: User

Before reading this article, you can also go to the Asp. Net Web API 2 series navigation to view http://www.cnblogs.com/aehyok/p/3446289.html

This article describes how ASP. NET Web APIs implement content negotiation.

The HTTP specification (RFC 2616) defines content negotiation as "selecting the best performance process for a given response when multiple representations are available ". The main mechanism of content negotiation in HTTP is the following request header:

  • Accept:Media types that can be received by the response, such as "application/json" and "application/xml", or custom media types, such as "application/vnd. example + xml ".
  • Accept-Charset:Acceptable character sets, such as UTF-8 or ISO 8859-1 ".
  • Accept-Encoding:The acceptable content encoding, such as "gzip ".
  • Accept-Language:The preferred natural language, such as "en-us ".

The server can also view other options of the HTTP request. For example, if the request contains an X-Requested-With header, it indicates that this is an AJAX request. If there is no Accept header, the server may use JSON by default.

This article describes how to use the Accept and Accept-Charset headers for Web APIs. (Currently, there is no built-in support for Accept-Encoding or Accept-Language .)

If the Web API controller returns a CLR-type response, the (request processing) pipeline serializes the returned value and writes it to the HTTP response body.

For example, consider the following controller actions:

 Product GetProduct( item = _products.FirstOrDefault(p => p.ID == (item ==  

The client may send such an HTTP request:

GET http:Host: localhost.:/json, text/javascript, *

The server may send the following response:

HTTP/ -Type: application/json; charset=utf--Length: :,:,:,:}

In this example, the client request (specified) contains JSON, Javascript, or "any format (*/*)". The server responds with a JSON representation of a Product object. Note that the Content-Type header in the response has been set to "application/json ".

The controller can also return an HttpResponseMessage object. To specify the CLR object of the response body, call the CreateResponse extension method:

 HttpResponseMessage GetProduct( item = _products.FirstOrDefault(p => p.ID == (item ==  

This option gives you more control over the Response Details. You can set status codes and add HTTP headers.

The object that serializes resources is called a media formatter. The media formatter is derived fromMediaTypeFormatterClass. Web APIs provide XML and JSON media formatters. Therefore, you can create custom formatters to support other media types.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.