In this article, I'll show you how to configure web api to return json data instead of xml data in ASP.NET MVC. By default, your web api returns xml data. To configure web api to return json object you need to open WebApiConfig.cs, then modify your code as shown belown.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.Formatters.JsonFormatter.MediaTypeMappings.Add(new RequestHeaderMapping("Accept", "text/html", StringComparison.InvariantCultureIgnoreCase, true, "application/json"));

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

Using RequestHeaderMapping works better, because it also puts Content-Type = application/json in the response header.

If you want to use query string to return json object you can modify as the following.

config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("json", "true", "application/json"));

Call web api: http://localhost:[port]/api/controller/action?json=true

http://localhost:49814/api/customer?json=true

http://localhost:49814/api/customer/getall?json=true