网页API路由和一个Web API帮助页面:如何避免重复条目 [英] Web API routing and a Web API Help Page: how to avoid repeated entries
问题描述
我收到我的<渲染重复条目href=\"http://blogs.msdn.com/b/yaohuang1/archive/2012/08/15/introducing-the-asp-net-web-api-help-page-$p$pview.aspx\"相对=nofollow>用不同的家长,像这样的,这指的是同样的方法的Web API帮助页面:
获取API / {} apiVersion / V1 /产品 - 获取所有产品
GET api/{apiVersion}/v1/Products - Gets all products
...
获取API / V1 /产品 - 获取所有产品
GET api/v1/Products - Gets all products
...
我有一些像这样的路由一个Web API页面:
I have a Web API page with some routing like this:
config.Routes.MapHttpRoute (
name: "DefaultVersionApi",
routeTemplate: "api/{apiVersion}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute (
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我原以为这个路由将使V1可选的,因此上述预期不会派生的文档。
I had thought that this routing would make the "v1" optional, so the derived documentation above is not expected.
(边栏:?要 API /产品
肯定是不行的,所以我不知道什么是不对的我缺少什么)
(sidebar: Going to api/products
certainly doesn't work, so I am not sure what is wrong with this. What am I missing?)
看来,真正的问题就是Web API帮助页面阅读的路线不正确,因为我认为 V1
和 {apiVersion}
不应该同时出现在同一个动作。 什么我在这里丢失?
It seems the real problem is that Web API Help Page is reading the routes improperly, as I thought v1
and {apiVersion}
should not both appear in the same action. What am I missing here?
推荐答案
看起来这是的ASP.NET Web API帮助页面的一个缺点。要解决,我改成排除呈现的文档中,这些无效的路由的看法。对于上面的例子中,我添加了这个Where子句中循环的 ApiGroup.cshtml ,改变
It seems like this is a shortcoming of the ASP.NET Web API help pages. To workaround, I changed the view to exclude these invalid routes from the rendered document. For the above example, I added this Where clause to the loop in ApiGroup.cshtml, changing
@foreach (var api in Model){
到
@foreach (var api in Model.Where(m => !m.Route.RouteTemplate.Contains(@"{apiVersion}"))){
这篇关于网页API路由和一个Web API帮助页面:如何避免重复条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!