title: ASPNetCore API Content negotiation not working
body: <p>I'm trying to get my api set up so it will respond with with XML or JSON depending upon the Accept header.</p>
  
  <p>I'm following the tutorial by Shawn W: <a href="https://wildermuth.com/2016/03/16/Content_Negotiation_in_ASP_NET_Core" rel="noreferrer">https://wildermuth.com/2016/03/16/Content_Negotiation_in_ASP_NET_Core</a></p>
  
  <p>It says to add a package to: <code>"Microsoft.AspNet.Mvc.Formatters.Xml": "6.0.0-rc1-final"</code></p>
  
  <p>But I couldn't find it so instead installed:
  <code>Microsoft.AspNetCore.Mvc.Formatters.Xml</code></p>
  
  <p>He says to add this to the config services section in Startup:</p>
  
  <pre><code>        // Add framework services.
          services
              .AddMvc(options =&gt; {
                  options.RespectBrowserAcceptHeader = true;
                  options.InputFormatters.Add(new XmlSerializerInputFormatter());
                  options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
              }).AddJsonOptions(options =&gt; {
                  // Force Camel Case to JSON
                  options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
              });
  </code></pre>
  
  <p>Then the browser is automatically meant to receive XML because by default it uses Accept: text/xml header, I don't get XML in my browser or by using postman. I get Json no matter what I set Accept header to.</p>
  
  <p>I've tried putting <code>[Produces("application/xml")]</code> on my controller and it returns a blank 200 OK page.</p>
  
  <p>How do I get my api to return Json by default, or XML if Accept is present?</p>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>I'm using this as my http get code:</p>
  
  <pre><code>    [HttpGet]
      public IActionResult Get() {
          var invoices = context.Invoices.ToList();
          var mappedInvoices = mapper.Map&lt;List&lt;DomainModels.Invoice&gt;, List&lt;Invoice&gt;&gt;(invoices);
          return Ok(mappedInvoices);
      }
  </code></pre>
  
  <p>And I'm trying to return this DTO:</p>
  
  <pre><code>public class Invoice : TrackedObject {
  
      public DateTime Date { get; set; }
  
      public decimal Total { get; set; }
  
      public string OrderNumber { get; set; }
  
  
      public PaymentType? PaymentType { get; set; }
  
      public ICollection&lt;InvoiceItem&gt; Items { get; set; }
  }
  public enum PaymentType {
      Cheque,
      Cash,
      Card,
      Account
  }
  </code></pre>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>If I swap out this:</p>
  
  <pre><code> // Add framework services.
          services
              .AddMvc(options =&gt; {
                  options.RespectBrowserAcceptHeader = true;
                  options.InputFormatters.Add(new XmlSerializerInputFormatter());
                  options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
              }).AddJsonOptions(options =&gt; {
                  // Force Camel Case to JSON
                  options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
              });
  </code></pre>
  
  <p>and replace with the below:</p>
  
  <pre><code>        services.AddMvc().AddXmlSerializerFormatters();
  </code></pre>
  
  <p>And then put <code>[Produces("application/xml")]</code> above my get method I get a 406 Not Acceptable response.</p>