Return either xml or json from MVC web api based on request

Given the following webapiconfig;

            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }

and this controller;

public class ProductsController : ApiController
    Product[] _products = new Product[] 
        new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
        new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
        new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 

    public IEnumerable<Product> GetAllProducts()
        return _products;

Using the URL http://localhost/api/Products I get a list of products in XML format.

What I would like to do is have the option to return either json or xml based on the request. So for json, it would be;


and for XML, it would be;




Is this possible and how would I achieve this functionality?

An alternative would be something like;


Yes you can achieve that with AddUriPathExtensionMapping

You can create routes like this:

  name: "Api UriPathExtension",
  routeTemplate: "api/{controller}.{extension}/{id}",
  defaults: new { id = RouteParameter.Optional, extension = RouteParameter.Optional }

  name: "Api UriPathExtension ID",
  routeTemplate: "api/{controller}/{id}.{extension}",
  defaults: new { id = RouteParameter.Optional, extension = RouteParameter.Optional }

Then you need to extend the formatters:

  config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json");
  config.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "text/xml");

Make sure to add reference to System.Net.Http.Formatting, as these methods are extension methods and intellisense won’t see them by default.

Remember in this example, you still have to issue the request with the appropriate content-type. However, if you want to have these directly available via the browser address bar you can map to “text/html”.

I wrote a blog post about all that a while ago – which should be helpful and take you into more details


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s