Please enable Javascript for better experience...
Changing default behaviour to get JSON response instead of XML response in Asp.Net Web API-C#
By Rahul Kumar Jha | Mar 28, 2015 | In Tips | Total Views [ 7052 ]
Taged In
(0 Like)
Rate

You can change default behaviour of response getting from Web API from XML type to JSON type by removing xml media type from XmlFormatter in Web API configuration.

The Problem

The problem is to change default behaviour of response returning from Web API as a result from XML type to JSON type.

A Quick Solution

By default response comes in XML format. You can change default behaviour of response getting from Web API to JSON format. Let's see how to do this with a simple example.

API Controller

Create a Web API Project. You will find ValuesController inherited from ApiController Add a Get(int id) method and run the application. Execute url api/values/5.

    public class ValuesController : ApiController
    {
        // GET api/values/5
        public string Get(int id)
        {
            return "value:" + id;
        }
    }

You will see an XML response on browser.

xml response

Web API Config

Add lines in WebApiConfig file to remove XML formatter from media type and you will see API response coming in JSON Format.

            //This will totally remove xml media type from formatter.
            var xmlMediaType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(x => x.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(xmlMediaType);

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

            //This will totally remove xml media type from formatter.
            var xmlMediaType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(x => x.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(xmlMediaType);

        }
    }

Use this if you want to totally remove XML format as API outcome.

JSON response

Hope this can help you.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer dotnet-concept.com

Public profile: user/profile/99900001


Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
 
 
 
 
 
Sponsors