Monday, 31 December 2018

How to return only XML from ASP.NET Web API Service irrespective of the Accept header value?

Include the following line in Register() method of WebApiConfig.cs file in App_Start folder. This line of code completely removes JsonFormatter which forces ASP.NET Web API to always return XML irrespective of the Accept header value in the client request. Use this technique when you want your service to support only XML and not JSON.

config.Formatters.Remove(config.Formatters.JsonFormatter);

With this change, irrespective of the Accept header value (application/xml or application/json), the Web API service is always going to return XML.

No comments:

Post a Comment