
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is Content Negotiation in Asp.Net webAPI C#?
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
- Related Articles
- What is ViewData in ASP .Net MVC C#?
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- How to use ViewBag in ASP .Net MVC C#?
- What is Negotiation Process?
- What are the three segments of the default route, that is present in ASP .Net MVC\nC#?
- What are the levels at which filters can be applied in ASP .Net MVC C#?
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- Negotiation Techniques
- What is parameter binding in C# ASP.NET WebAPI?
- What is serialization in C#.NET?
- Negotiation Skills in Project Management
- What is net investment hedge?
- What is the usage of DelegatingHandler in Asp.Net webAPI C#?
- International Conflict and Negotiation
