none
Change default content formatter in webapi RRS feed

  • Question

  • Is there an method attribute available to change the default content formatter in WEBAPI?

    I have a client that needs to consume XML but has no control over the headers to specify the content type for negotiation.

    Saturday, June 2, 2012 5:26 AM

Answers

  • It turns out adding the QueryStringMapping DOES result in the expected behavior.

    I've found that the specific return type I am using will only result in JSON formatted response.
    I have no idea why.
    But I did confirm this by changing the return type to an IEnumberable of string - which is formatted correctly to XML using the querystring parameter.


    foreach (var item in GlobalConfiguration.Configuration.Formatters) { if (typeof(XmlMediaTypeFormatter) == item.GetType()) { item.AddQueryStringMapping("format", "xml", "text/xml"); } }

    Example API call -
    http://localhost/api/foo?format=xml

    • Marked as answer by Jeremy Branham Saturday, June 2, 2012 5:32 PM
    • Edited by Jeremy Branham Saturday, June 2, 2012 5:35 PM updated with example
    Saturday, June 2, 2012 5:31 PM

All replies

  • Hi, 

    I guess you should be setting HttpRequest Accept method to "application/xml" 

    check with, 

    http://www.asp.net/web-api/overview/formats-and-model-binding/content-negotiation

    http://blogs.msdn.com/b/henrikn/archive/2012/04/22/asp-net-web-api-content-negotiation-and-accept-charset.aspx


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Saturday, June 2, 2012 7:44 AM
  • Unfortunately, one of the requirements is that the content negotiation cannot be done with a header value.

    I've tried adding a query string mapping to the XmlMediaTypeFormatter that is already in the GlobalConfiguration.Configuration.Formatters collection but it does not work the way I expect.

    This is what I have added to the application start -

                foreach (var item in GlobalConfiguration.Configuration.Formatters)
                {
                    if (typeof(XmlMediaTypeFormatter) == item.GetType())
                    {
                        item.AddQueryStringMapping("format", "xml", "text/xml");
                    }
                }

    I would think any controlller method would now support a query string parameter called 'format' that would perform content type negotiation.



    Saturday, June 2, 2012 2:01 PM
  • It turns out adding the QueryStringMapping DOES result in the expected behavior.

    I've found that the specific return type I am using will only result in JSON formatted response.
    I have no idea why.
    But I did confirm this by changing the return type to an IEnumberable of string - which is formatted correctly to XML using the querystring parameter.


    foreach (var item in GlobalConfiguration.Configuration.Formatters) { if (typeof(XmlMediaTypeFormatter) == item.GetType()) { item.AddQueryStringMapping("format", "xml", "text/xml"); } }

    Example API call -
    http://localhost/api/foo?format=xml

    • Marked as answer by Jeremy Branham Saturday, June 2, 2012 5:32 PM
    • Edited by Jeremy Branham Saturday, June 2, 2012 5:35 PM updated with example
    Saturday, June 2, 2012 5:31 PM
  • I found that the generated POCO Entity objects were not serializing with the default XML serializer that WEBAPI uses.

    By changing to the DataContract serializer, I was able to use the XML formatting.

    In the Application_Start()

    GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseDataContractSerial‌izer = true;

    Saturday, June 2, 2012 8:10 PM