locked
REST Web API on Windows Azure: How to remove the XmlFormatter RRS feed

  • Question

  • I am trying to get my REST Web API to ONLY serve up JSON regardless of the clients 'accept headers'. If I do not specify the 'accept headers' in e.g. Fiddler or the Chrome extension Advanced REST Client', my REST API always serves up data in JSON format, either against my local web server or against my Azure website. This is what I want.

    When I run the app locally in the web server that ships with VS2012, every browser will display JSON data. Perfect.

    However, when I run the app off of my Azure website, all browsers display the data in XML format; I want to force JSON on my Azure website.

    I have tried several different methods of removing the XML media formatter, but none of this is working. It seems that on my Azure website I cannot remove the XML media formatter.

    Any suggestions?

    protected void Application_Start()

    {

    //tried

    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

    // and...

    var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
    config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

    }

    Saturday, February 23, 2013 11:45 AM

All replies

  • Hi,

    Since the issue is related to Iaas, I have moved this thread to Web Sites forums for getting a better support.

    Thanks for your understanding.


    QinDian Tang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 25, 2013 3:14 AM
  • I haven't messed with this personally, but the default formatter should be JSON. Have you tried clearing all formatters like so?

    GlobalConfiguration.Configuration.Formatters.Clear();


    Jim Cheshire | Microsoft

    Monday, February 25, 2013 3:36 PM
  • Yes, I also tried removing all then adding just JSON, but no luck. Seems that the Xml formatter is not that easy to remove.

    GlobalConfiguration.Configuration.Formatters.Clear();
    GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.JsonMediaTypeFormatter());

    When I hit my Web API url with Chrome I get 2 different results, as seen by Fiddler. Same browser + same code, so it must be Azure IIS configuration versus my VS2012 web server instance.

    Web API on Azure: I get XML back
    Request Header: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Response Header Content-Type: application/xml; charset=utf-8

    Web API on Localhost: I get JSON back
    Request Header Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Response Content-Type: application/json; charset=utf-8

    I would prefer to just force JSON to the client, but I can always force JSON in the client Accept Request Headers to get the data format I want.

    Tuesday, February 26, 2013 2:45 AM
  • Can you possibly provide me with a small sample application that demonstrates this problem?


    Jim Cheshire | Microsoft

    Thursday, March 7, 2013 6:03 PM