What is the [Produces] attribute, and how does it restrict which content types a specific action can return regardless of what the client's Accept header requests?
Short answer
[Produces("application/json")] applied to a controller or action explicitly restricts the response to only the listed content type(s), overriding normal content negotiation entirely for that scope -- useful when an endpoint should always return a specific format (like a webhook…
Advertisement
AModel answer
[Produces("application/json")] applied to a controller or action explicitly restricts the response to only the listed content type(s), overriding normal content negotiation entirely for that scope -- useful when an endpoint should always return a specific format (like a webhook receiver that always expects JSON) regardless of what a misconfigured or unusual client's Accept header might otherwise request.
[Produces("application/json")]
[HttpPost("webhook")]
public IActionResult ReceiveWebhook(WebhookPayload payload) {
// Always returns JSON, even if the caller's Accept header requested XML
return Ok(new { received = true });
}Real-world example
A webhook receiver endpoint forces JSON output via [Produces] specifically because the calling service (a third-party payment provider) doesn't correctly negotiate content types, and JSON must always be returned regardless of its actual Accept header.
Likely follow-ups
- How does [Produces] interact with content negotiation for the request body versus the response?
- What's the difference between [Produces] at the controller level versus the action level?
Related topics