Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Given the following webapiconfig;

config.Routes.MapHttpRoute(             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;

http://localhost/api/Products.json 

and for XML, it would be;

http://localhost/api/Products.xml 

likewise;

http://localhost/api/Products.json/1/ http://localhost/api/Products.xml/1/ 

Is this possible and how would I achieve this functionality?

An alternative would be something like;

http://localhost/api/json/Products/ 
like image 587
ChrisBint Avatar asked Oct 24 '12 16:10

ChrisBint


People also ask

Can we return XML in Web API?

the Web API 2 framework defaults to XML but uses the client's accept header to determine the return type format. If XML is required for every response then configure Web API to always return XML.

Does Web API support XML and JSON?

Web API provides media-type formatters for both JSON and XML. The framework inserts these formatters into the pipeline by default. Clients can request either JSON or XML in the Accept header of the HTTP request.


1 Answers

Yes you can achieve that with AddUriPathExtensionMapping

You can create routes like this:

routes.MapHttpRoute(   name: "Api UriPathExtension",   routeTemplate: "api/{controller}.{extension}/{id}",   defaults: new { id = RouteParameter.Optional, extension = RouteParameter.Optional } );  routes.MapHttpRoute(   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 http://www.strathweb.com/2012/04/different-mediatypeformatters-for-same-mediaheadervalue-in-asp-net-web-api/

like image 99
Filip W Avatar answered Oct 14 '22 12:10

Filip W