【发布时间】:2015-02-06 09:17:49
【问题描述】:
Asp.net 默认返回类型是 XML。但我可以在配置设置中更改它。
public static void Register(HttpConfiguration config)
{
config.Formatters.Clear();
// config.Formatters.Add(new XmlMediaTypeFormatter());
config.Formatters.Add(new JsonMediaTypeFormatter());
}
我的控制器是:
public class ProductController: ApiController
{
public IEnumerable<Product> Get()
{
return new List<Product> {
new Product {Name = "p1", Price = 10},
new Product {Name = "p2", Price = 20}
};
}
}
现在我想这样做:
- 用户应使用参数指定返回类型。
-
http://domain/product/get(格式为 xml 或 json)
我不想更改我的控制器操作。
有什么方法可以通过 Route 参数或任何其他级别来做到这一点?
【问题讨论】:
标签: c# asp.net json asp.net-mvc asp.net-web-api