2

I'm using asp.net core 2.2 and trying to allow additional content-type value for XML. So I configured in Startup.cs like this.

services.AddMvc(options =>
        {
            options.ReturnHttpNotAcceptable = true;
            options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
            options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); 

In fact, I ended up getting 415 Unsupported Media Type status in postman. Has anyone succeeded with adding XML content-type value?

AuthorsController.cs:

    [HttpPost]
    public ActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
    {
        ...
    }

enter image description here

enter image description here

bbusdriver
  • 1,577
  • 3
  • 26
  • 58

1 Answers1

0

You need to set RespectBrowserAcceptHeader to true to allow content negotiation.

// Add XML Content Negotiation
config.RespectBrowserAcceptHeader = true;


services.AddMvc(options => {
    //Enable this to allow content negotiation.
    options.RespectBrowserAcceptHeader = true;

    options.ReturnHttpNotAcceptable = true;
    options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
    options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2); 
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thanks for your answer. Unfortunately, I still see `415` response. Is `options.RespectBrowserAcceptHeader = true;` tested in 2.2? – bbusdriver Apr 17 '19 at 05:41
  • so services.AddMvc(options => { options.InputFormatters.Add(new XmlSerializerInputFormatter(options)); }); is used for converting null or empty value to type 'enum' or is there any other way to convert null or empty value to type enum?? – anirudh talluri Dec 08 '21 at 13:23