Web API Controllers & Content Negotiation in ASP.NET Core
π‘ Concept Name
Web API Controllers & Content Negotiation in ASP.NET Core
π Quick Intro
Web API controllers are built for serving data to clients like mobile apps or frontend frameworks. They return data formats like JSON or XML rather than views. Content negotiation lets clients receive the format they ask for.
π§ Analogy / Short Story
Imagine you visit a multilingual helpdesk and ask your query in English. If the staff replies in Hindi, youβre confused. Content negotiation ensures you get the reply in the same language you asked for β just like the server responding in the format (JSON/XML) your client understands.
π§ Technical Explanation
Web API controllers in ASP.NET Core use the [ApiController]
attribute and return data directly via ActionResult<T>
.
Content negotiation uses Accept headers (e.g., application/json or application/xml) to determine the formatter that ASP.NET Core uses to serialize the response.
π― Purpose & Use Case
- β Build RESTful APIs for SPAs and mobile clients
- β Return flexible data formats using Accept headers
- β Integrate JSON/XML easily with minimal config
- β Clean separation between UI and data layers
- β Enables APIs to be consumed by diverse clients
π» Real Code Example
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet]
[Produces("application/json", "application/xml")]
public ActionResult<Product> GetProduct()
{
var product = new Product { Id = 1, Name = "Laptop", Price = 999.99 };
return Ok(product);
}
}

β Interview Q&A
Q1: What is the use of [ApiController]?
A: Enables model validation, attribute routing, and improved API behavior.
Q2: How does content negotiation work?
A: Based on the client's Accept header, the server chooses the appropriate formatter.
Q3: What are default formatters in ASP.NET Core?
A: JSON (via System.Text.Json or Newtonsoft.Json).
Q4: Can we return XML?
A: Yes, by adding services.AddControllers().AddXmlSerializerFormatters()
.
Q5: What is IActionResult vs ActionResult<T>?
A: IActionResult is flexible, ActionResult<T> combines type safety + flexibility.
Q6: What is [Produces] attribute?
A: Specifies the response content types an action can return.
Q7: How to enforce only JSON response?
A: Use [Produces("application/json")] and remove other formatters.
Q8: Where is content negotiation used?
A: In Web APIs to deliver correct format to client (browser, mobile, etc.).
Q9: How to test content negotiation?
A: Use Postman or curl with Accept header (e.g., Accept: application/xml).
Q10: Does content negotiation apply to MVC views?
A: No, it is relevant for Web APIs returning data, not views.
Q1: What does content negotiation depend on?
- A. Controller name
- B. Accept header
- C. URL extension
- D. Browser type
Q2: What attribute is essential for ASP.NET Core Web API controllers?
- A. [Controller]
- B. [HttpApi]
- C. [ApiController]
- D. [WebApi]
π‘ Bonus Insight
Content negotiation is powerful for building globalized APIs. You can plug in custom formatters (like CSV or YAML) to extend its functionality beyond JSON/XML β giving clients even more control over what they receive.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!