I have the following class as a return type from an action on a MVC4 WebApi controller:
[XmlRoot(Namespace = "http://ift.tt/1sFEtL7")]
public class Result
{
public string Some;
public int Simple;
public string Fields;
}
And in WebApiConfig I have:
config.Formatters.Clear();
config.Formatters.Add(new XmlMediaTypeFormatter());
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
config.Formatters.XmlFormatter.UseXmlSerializer = true;
This is because the client requires Xml by default, without having to set the Accept header. I've tried it with and without that block of code, and with or without setting UseXmlSerializer to true. It makes no difference using XmlRoot, XmlType or DataContract
Yet the output is always:
<Result>
<Some>String</Some>
<Simple>7</Simple>
<Fields>String</Fields>
</Result>
Never:
<Result xmlns="http://ift.tt/1sFEtL7">
<Some>String</Some>
<Simple>7</Simple>
<Fields>String</Fields>
</Result>
It's using the attribute because if I change it to:
[XmlRoot(ElementName = "TheResult")]
I get:
<TheResult>
<Some>String</Some>
<Simple>7</Simple>
<Fields>String</Fields>
</TheResult>
There's a similar question here:
XML Namespaces in MVC4 Web API
Which has no answer, and everybody is asking how to get rid of namespaces.
Yet I don't see them at all!
Is there some sort of global switch somewhere?
Why can't I set a default namespace on the Xml output from my Web API?
No comments:
Post a Comment