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

2

2

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?

Untouchable answered 12/8, 2022 at 18:51 Comment(0)
M
2

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

[Consumes("application/json")]

Melodrama answered 2/2, 2023 at 23:51 Comment(0)
S
2

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.

Solano answered 16/8, 2022 at 7:42 Comment(1)
The Op asked for content type of the request, not the response.Melodrama
M
2

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

[Consumes("application/json")]

Melodrama answered 2/2, 2023 at 23:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.