How to accept JSON and XML both in Web API post method in .Net 5

Viewed 258

I have an API method that has a FromBody Model and it's working properly with JSON data as input but when I use XML as an input parameter, it throws an error. I tried using XMLserialiser in the startup file.

services.AddControllers(options => 
    { 
        options.RespectBrowserAcceptHeader = true;
        options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
        options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
    })

Nothing worked.

 [HttpPost]
public IActionResult Post([FromBody]Model model)
{
    return Ok();
}

Any help would be appreciated.

1 Answers

For any one still trying to figure this out you can take a look here as it solved my own issue Combine Json and Xml. Following that simple tutorial i was able to post either content type as Json or xml.

Related