C# 根据请求从 MVC web api 返回 xml 或 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13053485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return either xml or json from MVC web api based on request
提问by ChrisBint
Given the following webapiconfig;
鉴于以下 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/ProductsI get a list of products in XML format.
使用 URL,http://localhost/api/Products我获得了 XML 格式的产品列表。
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;
我想做的是可以选择根据请求返回 json 或 xml。所以对于 json,它会是;
http://localhost/api/Products.json
and for XML, it would be;
对于 XML,它将是;
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/
采纳答案by Filip W
Yes you can achieve that with AddUriPathExtensionMapping
是的,你可以做到这一点 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.
确保添加对 的引用System.Net.Http.Formatting,因为这些方法是扩展方法,默认情况下智能感知不会看到它们。
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".
请记住,在此示例中,您仍然必须使用适当的内容类型发出请求。但是,如果您想通过浏览器地址栏直接获得这些内容,您可以映射到“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/
不久前我写了一篇关于所有这些的博客文章 - 这应该会有所帮助并带你了解更多细节http://www.strathweb.com/2012/04/different-mediatypeformatters-for-same-mediaheadervalue-in-asp-网络-web-api/

