役に立ちましたか?

質問

What is Content Negotiation in Asp.Net webAPI C#?

CsharpServer Side ProgrammingProgramming

Content negotiation is the process of selecting the best representation for a given response when there are multiple representations available. Means, depending on the Accept header value in the request, the server sends the response. The primary mechanism for content negotiation in HTTP are these request headers −

Accept − Which media types are acceptable for the response, such as "application/json," "application/xml," or a custom media type such as "application/vnd.example+xml"

Accept-Charset − Which character sets are acceptable, such as UTF-8 or ISO 8859-1.

Accept-Encoding − Which content encodings are acceptable, such as gzip.

Accept-Language − The preferred natural language, such as "en-us".

The server can also look at other portions of the HTTP request. For example, if the request contains an X-Requested-With header, indicating an AJAX request, the server might default to JSON if there is no Accept header.

In content negotiation, the pipeline gets the IContentNegotiator service from the HttpConfiguration object. It also gets the list of media formatters from the HttpConfiguration.Formatters collection.

Next, the pipeline calls IContentNegotiator.Negotiate, passing in −

  • The type of object to serialize
  • The collection of media formatters
  • The HTTP request

The Negotiate method returns two pieces of information −

  • Which formatter to use
  • The media type for the response

If no formatter is found, the Negotiate method returns null, and the client receives HTTP error 406 (Not Acceptable).

Let us consider StudentController like below.

using DemoWebApplication.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace DemoWebApplication.Controllers{
   public class StudentController : ApiController{
      List<Student> students = new List<Student>{
         new Student{
            Id = 1,
            Name = "Mark"
         },
         new Student{
            Id = 2,
            Name = "John"
         }
      };
   }
}

One of the standards of the RESTful service is that, the client should have the ability to decide in which format they want the response - XML, JSON etc. A request that is sent to the server includes an Accept header. Using the Accept header the client can specify the format for the response. For example

Accept: application/xml returns XML
Accept: application/json returns JSON

The below output shows the response is of XML when we pass the Accept Header as application/XML.

The below output shows the response is of JSON when we pass the Accept Header as application/JSON.

When the response is being sent to the client in the requested format, notice that the Content-Type header of the response is set to the appropriate value. For example, if the client has requested application/xml, the server send the data in XML format and also sets the Content-Type=application/xml.

We can also specify quality factor. In the example below, xml has higher quality factor than json, so the server uses XML formatter and formats the data in XML. application/xml;q=0.8,application/json;q=0.5

raja
Published on 24-Sep-2020 14:52:27
Advertisements
役に立ちましたか?
所属していません Tutorialspoint
scroll top