How to reject requests in a .NET Core API based on the values sent in the Accept header?

1k Views Asked by At

Reading about how to return status code 406 in a .NET Core API for requests that require a response in a format not supported by my API, I found that I need to set the following option in the AddMvcCore method:

services.AddMvcCore(options =>
{
    options.ReturnHttpNotAcceptable = true;
});

However, I noticed that even if a client requests for an unsupported format, the request is forwarded to the endpoint and only after processing done in Controller that the status code 406 is returned.

I would like to know if .NET Core has a ready-made solution that blocks the request, that is, returns the status code 406 without having to execute the endpoint code. I have found solutions in which Middleware can be written to perform such a task, but I would really like to know if there is an alternative built into the framework itself.

0

There are 0 best solutions below