I have a .net core 3.1 web api. I have tried the following but it always turns up null when it hits it
[HttpPost]
public IActionResult ReturnXmlDocument(HttpRequestMessage request)
{
var doc = new XmlDocument();
doc.Load(request.Content.ReadAsStreamAsync().Result);
return Ok(doc.DocumentElement.OuterXml.ToString());
}
It doent even hits it during debugging also it shows a 415 error in fiddler.
Asp.net core no longer uses HttpRequestMessage or HttpResponseMessage.
So, if you want to accept a xml format request, you should do the below steps:
1.Install the Microsoft.AspNetCore.Mvc.Formatters.Xml NuGet package.
2.Call AddXmlSerializerFormatters In Startup.ConfigureServices.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddXmlSerializerFormatters();
}
3.Apply the Consumes attribute to controller classes or action methods that should expect XML in the request body.
[HttpPost]
[Consumes("application/xml")]
public IActionResult Post(Model model)
{
return Ok();
}
For more details, you can refer to the offical document of Model binding