Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statuscode 406 (Not Acceptable) in ASP.NET Core

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?

like image 725
Johann Heinzelreiter Avatar asked May 28 '17 15:05

Johann Heinzelreiter


People also ask

What is the meaning of 406 (Not Acceptable) status code?

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.

What is the HTTP status code for NET Core?

.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.

What is the content negotiation status code 406 for a REST service?

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).

What is the response code for rewriterule 406?

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.


3 Answers

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();
}
like image 103
Matrox911 Avatar answered Oct 11 '22 17:10

Matrox911


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;
});
like image 16
poke Avatar answered Oct 11 '22 18:10

poke


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());
});
like image 4
R. Richards Avatar answered Oct 11 '22 18:10

R. Richards