REST services should provide content negotiation. This means that clients send an Accept header that contains the desired content type of the response. If the service does not support this media type, it should respond with status code 406 (Not Acceptable).
I try to map this behavior to ASP.NET Core. ASP.NET core returns a JSON document, if it doesn't recognize the media type in the Accept header. In previous versions of the framework the behavior described above could be achieved by adding a special output formatter to the configuration:
public void ConfigureServices(IServiceCollection services) {
services.AddMvc(options => {
options.OutputFormatters.Insert(0, new HttpNotAcceptableOutputFormatter());
});
}
Unfortunately, HttpNotAcceptableOutputFormatter
was removed from the ASP.NET Core framework after RC1. Is there any replacement for this class in the current version of the framework?
This means that clients send an Accept header that contains the desired content type of the response. If the service does not support this media type, it should respond with status code 406 (Not Acceptable). I try to map this behavior to ASP.NET Core. ASP.NET core returns a JSON document, if it doesn't recognize the media type in the Accept header.
.NET Core has inbuilt support for all basic HTTP Status codes and they are easy to configure for almost all standard HTTP status codes. RFC7231 defines the specification for this HTTP Status code in detail. The HTTP 200 (OK) status code indicates success.
REST services should provide content negotiation. This means that clients send an Accept header that contains the desired content type of the response. If the service does not support this media type, it should respond with status code 406 (Not Acceptable).
Notice the R=406 flag at the end of the RewriteRule, which explicitly states that the response code should be 406, indicating to user agents that the resource exists, but the explicit Accept- headers could not be fulfilled.
I had this before:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
Then I change it to AddMvcCore()
instead of AddMvc()
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore();
}
Finally I had that issue with the Response 406 so what I did was to add .AddJsonFormatters()
to services.AddMVCCore()
and my API worked again.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddJsonFormatters();
}
In such cases, it’s a good idea to find the commit that removed the functionality, to see what it likely got replaced with. In this case, HttpNotAcceptableOutputFormatter
was removed with this commit to fix issue #4612:
Alter content negotiation algorithm so that it can be configured (via MvcOptions) to always respect an explicit Accept header.
What it was replaced with is MvcOptions.ReturnHttpNotAcceptable
, which is a setting on the MvcOptions
that you configure when adding MVC with AddMvc
.
So your code should become like this:
services.AddMvc(options =>
{
options.ReturnHttpNotAcceptable = true;
});
You add this to the ConfigureService
method in the Startup
class.
services.AddMvc(options =>
{
options.ReturnHttpNotAcceptable = true;
// If you need to add support for XML
// options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With