How can we make sure that Web API returns JSON data only?

How can we make sure that Web API returns JSON data only?

In Web API project, we need to use the WebApiConfig.cs class.

Just add the following in App_Start / WebApiConfig.cs class in my MVC Web API project.

config.Formatters.JsonFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue(“text/html”) );

That makes sure you get JSON on most queries, but you can get XML when you send text/xml.
If you need to have the response Content-Type as application/json