Return either xml or json from MVC web api based o

2020-02-02 07:15发布

问题:

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/

回答1:

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/