Tuesday 24 October 2017

how to return json data from web api

Open WebApiConfig.cs file in solution and add mentioned line in it as shown in example.


var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.XmlFormatter);
Or
 config.Formatters.Remove(config.Formatters.XmlFormatter);

Or

public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//To produce JSON format add this line of code
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(newMediaTypeHeaderValue("text/html"));
}


No comments:

Post a Comment