Posts Tagged ‘webapi’

(using MVC4 beta)

In some cases it is not easy to alter the header values to negotiate the content type returned from WEBAPI.

There is a QueryStringMapper object you can add to the Media Type Formatter objects that allow you to use a query string to negotiate the content format.

In the example below,¬† I am adding a query string parameter ‘format’ to the mapping. When ‘format’ == ‘xml’, the content type is ‘text/xml’ and the XmlMediaTypeFormatter is used.

I run this in the ‘Application_Start’ method of the Global.asax¬†

            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