Monday, 31 December 2018

How to return only JSON 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 XmlFormatter which forces ASP.NET Web API to always return JSON irrespective of the Accept header value in the client request.
With this change, irrespective of the Accept header value (application/xml or application/json), the Web API service is always going to return JSON.

config.Formatters.Remove(config.Formatters.XmlFormatter);

Use this technique when you want your service to support only JSON and not XML. 

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


No comments:

Post a Comment