How to configure ASP.NET Core Web API to only accept `application/json`?

3.2k Views Asked by At

I would like to configure my ASP.NET Core Web API using .NET 6 to only accept application/json as the accept header value. How can I configure that?

2

There are 2 best solutions below

0
Adam Hardy On BEST ANSWER

Took me a while, but adding the consumes attribute (rather than produces) will do the trick for you.

[Consumes("application/json")]

1
Chen On

Set [Produces("application/json")] for controller which can achieve the effect you want.

[Produces("application/json")]
public class WeatherForecastController : ControllerBase
{
}

enter image description here

For more details, you can refer to this document.