1

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.

Elaine
  • 1,288
  • 5
  • 17
  • 35
  • What XML are you sending? – CodeCaster Nov 01 '16 at 10:16
  • @CodeCaster I have just edited the question to add more information. – Elaine Nov 01 '16 at 11:07
  • 1
    Your XML has a namespace. Is your model annotated with that namespace? See also http://stackoverflow.com/questions/12617471/adjust-mvc-4-webapi-xmlserializer-to-lose-the-namespace – CodeCaster Nov 01 '16 at 11:30
  • @CodeCaster I still need this namespace information to do schema validation, so I have to create custom XmlFormater? yet not found any posts about this in .net core. I'm considering just get string and then do manual formatting in the action, yet it doesn't even allow this with content-type="text/xml". – Elaine Nov 01 '16 at 11:43
  • @CodeCaster I think you are right, after I commented out all namespace attributes in the proxy class (migrated from .net 4.5 wcf), and removed ns from xml request, now I can get it perfectly. Much appreciate. – Elaine Nov 01 '16 at 12:04
  • 1
    If the namespaces on the incoming XML and the model used to deserialize into match, then it should work as well. :) – CodeCaster Nov 01 '16 at 12:24
  • @CodeCaster i just tried, with the same code I posted in the question, but just add a [System.Xml.Serialization.XmlRootAttribute("OrderRequest", ...], and now it works also. Why in wcf, this is not needed, but here is – Elaine Nov 02 '16 at 04:24
  • In wcf, this is not needed, could be that wcf wraps Soap as root element. I will add another wrapper to decouple from proxy class definitions. Thank you, really helps, why not add an answer and I can accept it :) others could have the same problem I had. – Elaine Nov 02 '16 at 04:31

0 Answers0