title: .Net Core MVC - cannot get 406 - Not Acceptable, always returns 200 OK with Json
body: <p>I want my application to respect browser accept header and return 406, if it does not match the response format.</p>
  
  <p>I have this options set in Mvc configuration:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// This method gets called by the runtime. Use this method to add services to the container.
      /// &lt;/summary&gt;
      /// &lt;param name="services"&gt;The collection of the services.&lt;/param&gt;
      /// &lt;returns&gt;The provider of the service.&lt;/returns&gt;
      public IServiceProvider ConfigureServices(IServiceCollection services)
      {
          // add mvc services
          services.AddMvc(options =&gt;
                  {
                      options.RespectBrowserAcceptHeader = true;
                      options.ReturnHttpNotAcceptable = true;
  
                      options.CacheProfiles.Add(
                          "CodebookCacheProfile",
                          new CacheProfile()
                          {
                              Duration = (int)TimeSpan.FromDays(1).TotalSeconds
                          });
                  })
                  .AddControllersAsServices()
                  .AddJsonOptions(options =&gt;
                  {
                      options.SerializerSettings.Converters.Add(new StringEmptyToNullConverter());
                      options.SerializerSettings.Converters.Add(new StringEnumConverter(true));
                  });
  
          // add response compression services
          services.AddResponseCompression(options =&gt;
          {
              options.EnableForHttps = true;
              options.Providers.Add&lt;GzipCompressionProvider&gt;();
          });
  
          // add application services
          services.AddSwaggerDoc()
                  .AddConfiguration(configuration)
                  .AddModel()
                  .AddDataAccess(configuration)
                  .AddAuthentication(configuration)
                  .AddDomainServices()
                  .AddSchedulerContainer(() =&gt; serviceProvider);
  
          // initialize container
          serviceProvider = services.CreateServiceProvider();
  
          return serviceProvider;
      }
  </code></pre>
  
  <p>When I try to send request like this: (with Accept header set on whatever, for example "text/xml")</p>
  
  <p>I always get 200 OK - with "application/json"
  <a href="https://i.stack.imgur.com/tZZH9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tZZH9.png" alt="Request with specified accept header and the response in json format"></a></p>
  
  <p>My <em>CountriesController</em> looks like this:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// REST API controller for actions with countries.
  /// &lt;/summary&gt;    
  [AllowAnonymous]
  [Area(Area.Common)]
  [Route("[area]/Codebooks/[controller]")]
  [ResponseCache(CacheProfileName = "CodebookCacheProfile")]
  public class CountriesController : ApiController
  {
      private readonly ICountryService countryService;
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="CountriesController" /&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="countryService"&gt;The country service.&lt;/param&gt;
      public CountriesController(ICountryService countryService)
      {
          this.countryService = countryService ?? throw new ArgumentNullException(nameof(countryService));
      }
  
      /// &lt;summary&gt;
      /// Gets countries by search settings.
      /// &lt;/summary&gt;
      /// &lt;response code="200"&gt;The countries was returned correctly.&lt;/response&gt;
      /// &lt;response code="401"&gt;The unauthorized access.&lt;/response&gt;
      /// &lt;response code="406"&gt;The not acceptable format.&lt;/response&gt;
      /// &lt;response code="500"&gt;The unexpected error.&lt;/response&gt;
      /// &lt;param name="countrySearchSettings"&gt;The search settings of the country.&lt;/param&gt;
      /// &lt;returns&gt;Data page of countries.&lt;/returns&gt;        
      [HttpGet]
      [ProducesResponseType(typeof(IDataPage&lt;Country&gt;), StatusCodes.Status200OK)]
      [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
      [ProducesResponseType(typeof(void), StatusCodes.Status406NotAcceptable)]
      [ProducesResponseType(typeof(ApiErrorSummary), StatusCodes.Status500InternalServerError)]
      [SwaggerOperation("SearchCountries")]
      public IDataPage&lt;Country&gt; Get([FromQuery(Name = "")] CountrySearchSettings countrySearchSettings)
      {
          return countryService.Search(countrySearchSettings);
      }
  
      /// &lt;summary&gt;
      /// Gets a country.
      /// &lt;/summary&gt;
      /// &lt;response code="200"&gt;The country was returned correctly.&lt;/response&gt;
      /// &lt;response code="400"&gt;The country code is not valid.&lt;/response&gt;
      /// &lt;response code="401"&gt;The unauthorized access.&lt;/response&gt;
      /// &lt;response code="406"&gt;The not acceptable format.&lt;/response&gt;
      /// &lt;response code="500"&gt;The unexpected error.&lt;/response&gt;
      /// &lt;param name="countryCode"&gt;The code of the country.&lt;/param&gt;
      /// &lt;returns&gt;Action result.&lt;/returns&gt;   
      [HttpGet("{countryCode}")]
      [ProducesResponseType(typeof(Country), StatusCodes.Status200OK)]
      [ProducesResponseType(typeof(ApiValidationErrorSummary), StatusCodes.Status400BadRequest)]
      [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
      [ProducesResponseType(typeof(void), StatusCodes.Status406NotAcceptable)]
      [ProducesResponseType(typeof(ApiErrorSummary), StatusCodes.Status500InternalServerError)]
      [SwaggerOperation("GetCountry")]
      public IActionResult Get(string countryCode)
      {
          var country = countryService.GetByCode(countryCode);
          return Ok(country);
      }
  }
  </code></pre>
  
  <p>Do you have any idea why the request Accept header is always ignored and the response is always 200 OK with correct Json data?
  What am I missing? I thought that the setting of <em>RespectBrowserAcceptHeader</em>  and <em>ReturnHttpNotAcceptable</em> would do the thing... but apparently not.
  Why it always falls back to the default Json formatter?</p>