WebAPI in .net core can elegantly handle Json request, but it seems not able to handle XML content-type natively?
I did some search, and got only the post about this, http://www.strathweb.com/2015/04/asp-net-mvc-6-formatters-xml-browser-requests/
Yet, I tried as below, not successful
services
.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto;
})
.AddMvcOptions(options =>
{
//var jsonFormatter = options.InputFormatters.First(o => o.GetType() == typeof(JsonInputFormatter));
//if (jsonFormatter != null) { options.InputFormatters.Remove(jsonFormatter); }
options.InputFormatters.Add(new XmlSerializerInputFormatter());
options.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());
options.RespectBrowserAcceptHeader = true; // false by default
options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter(
new System.Xml.XmlWriterSettings
{
ConformanceLevel = System.Xml.ConformanceLevel.Auto,
WriteEndDocumentOnClose = true
}
));
var a = options.FormatterMappings.GetMediaTypeMappingForFormat("text/xml");
})
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters()
;
Below is my controller action
// POST api/values
[HttpPost("Order")]
public string OrderRetrievea([FromBody] OrderRequest request)
{
}
Content-type is set to text/xml.
Xml request:
<?xml version="1.0"?>
<OrderRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.myco.org/ns">
....
</OrderRequest>
It(request)'s always null.