我如何获得的ASP.NET Web API与版本控制和帮助页面的扩展工作? [英] How do I get ASP.NET Web API working with versioning and the Help Page extension?
问题描述
我已经实现了一个版本的框架到我的WebAPI应用程序,非常希望得到它与微软新的帮助页面的扩展工作。
<一个href=\"http://nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage\">Microsoft.AspNet.WebApi.HelpPage
很简单,我不知道如何让他们两个一起工作。我有2个项目:
- AdventureWorks.Api(主要主机/应用程序根)
- AdventureWorks.Api.v1(包含API的第一个版本的类库)
该版本按预期工作。
我试过根应用程序安装包HelpPage,当我浏览到帮助页面,它似乎没有任何控制器都被发现。在内部,我相信它使用:
Configuration.Services.GetApiExplorer()。ApiDescriptions
这不返回任何结果,所以我得到一个错误。
任何人都可以帮助我获得这两个包一起工作的?
编辑:
在开始的时候,我不知道这是一个路由的问题,但最近的评论似乎并非如此。这里是我的RouteConfig.cs
公共类RouteConfig
{
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapHttpRoute(
名称:SldExportAliasApi
routeTemplate:API / V {}版本/ SLD出口/(编号),
默认:新{ID = RouteParameter.Optional,控制器=导出}
); routes.MapHttpRoute(
名称:LinkRoute
routeTemplate:API / V {}版/连接/ {系统} / {部署} / {}看法,
默认:新{控制器=链接}
); routes.MapHttpRoute(
名称:DefaultSubParameterApi
routeTemplate:API / V {版本} / {控制器} / {ID} / {}参数,
默认:新{ID = RouteParameter.Optional,参数= RouteParameter.Optional}
); routes.MapHttpRoute(
名称:DefaultApi
routeTemplate:API / V {版本} / {控制器} / {行动} / {ID}
默认:新{行动=索引,ID = RouteParameter.Optional}
); routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
}
}
您需要从项目AdventureWorks.Api.v1项目得到了文档的XML文件,并将其放置在AdventureWorks.Api项目的bin文件夹:
然后将这些行添加到您的Application_Start方法:
//启用API版本
GlobalConfiguration.Configuration.Services.Replace(typeof运算(IHttpControllerSelector),新RouteVersionControllerSelector(GlobalConfiguration.Configuration));
GlobalConfiguration.Configuration.Services.Replace(typeof运算(IApiExplorer),新VersionedApiExplorer(GlobalConfiguration.Configuration));
GlobalConfiguration.Configuration.Services.Replace(typeof运算(IDocumentationProvider)
新XmlCommentDocumentationProvider(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly()的GetName()。codeBase类)+
\\\\ Adventure.Api.v1.XML));
然后就可以用的文档查看您的API。
有时版本号不获取要正确地拾起,并通过???替换
要解决这个地址:
如果(api.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace!= NULL)
{
。变种的versionName = api.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace.Replace(控制器,)(。).Split最后();
api.RelativePath = api.RelativePath.Replace(V ???的versionName);
}
您ApiGroup.cshtml正是在这个地方:
@foreach(以VAR模型API)
{
如果(api.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace!= NULL)
{
。变种的versionName = api.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace.Replace(控制器,)(。).Split最后();
api.RelativePath = api.RelativePath.Replace(V ???的versionName);
}
&所述; TR&GT;
&LT; TD类=API名称&GT;&LT; A HREF =@ Url.Action(API,帮助,新的{apiId = api.GetFriendlyId()})&GT; @ api.HttpMethod。方法@ api.RelativePath&下; / A&GT;&下; / TD&GT;
&LT; TD类=API的文档&GT;
@if(api.Documentation!= NULL)
{
&所述p为H.; @ api.Documentation&下; / P&GT;
}
其他
{
&LT; P&GT;没有可用的文档和LT; / P&GT;
}
&LT; / TD&GT;
&LT; / TR&GT;
}
这应该做的伎俩!
I've implemented a versioning framework into my WebAPI application, and would very much like to get it working with the new Help Page extension from Microsoft.
Microsoft.AspNet.WebApi.HelpPage
Quite simply, I don't know how to get them both working together. I have 2 projects:
- AdventureWorks.Api (The main host/root application)
- AdventureWorks.Api.v1 (A class library containing the first version of the API)
The versioning works as expected.
I've tried installing the HelpPage package on the root application, and when I browse to the help page, it appears none of the controllers are being found. Internally I believe it uses:
Configuration.Services.GetApiExplorer().ApiDescriptions
This returns no results, so I get an error.
Can anyone assist me in getting both of these packages working together?
Edit: In the beginning, I wasn't sure this was a routing problem, but recent comments seem to suggest otherwise. Here is my RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "SldExportAliasApi",
routeTemplate: "api/v{version}/sld-export/{id}",
defaults: new { id = RouteParameter.Optional, controller = "Export" }
);
routes.MapHttpRoute(
name: "LinkRoute",
routeTemplate: "api/v{version}/link/{system}/{deployment}/{view}",
defaults: new { controller = "Link" }
);
routes.MapHttpRoute(
name: "DefaultSubParameterApi",
routeTemplate: "api/v{version}/{controller}/{id}/{param}",
defaults: new { id = RouteParameter.Optional, param = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v{version}/{controller}/{action}/{id}",
defaults: new { action = "Index", id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
You need to get a documentation XML file from your project AdventureWorks.Api.v1 project and place it in the bin folder of the AdventureWorks.Api project:
Then add these lines to your Application_Start method:
// enable API versioning
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new RouteVersionControllerSelector(GlobalConfiguration.Configuration));
GlobalConfiguration.Configuration.Services.Replace(typeof(IApiExplorer), new VersionedApiExplorer(GlobalConfiguration.Configuration));
GlobalConfiguration.Configuration.Services.Replace(typeof(IDocumentationProvider),
new XmlCommentDocumentationProvider(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) +
"\\Adventure.Api.v1.XML"));
Then you can view your API with the documentation.
Sometimes the version number does not get to be picked up correctly, and replaced by ???
To fix this add:
if (api.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace != null)
{
var versionName = api.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace.Replace(".Controllers", "").Split('.').Last();
api.RelativePath = api.RelativePath.Replace("v???", versionName);
}
to your ApiGroup.cshtml exactly at this place:
@foreach (var api in Model)
{
if (api.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace != null)
{
var versionName = api.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace.Replace(".Controllers", "").Split('.').Last();
api.RelativePath = api.RelativePath.Replace("v???", versionName);
}
<tr>
<td class="api-name"><a href="@Url.Action("Api", "Help", new { apiId = api.GetFriendlyId() })">@api.HttpMethod.Method @api.RelativePath</a></td>
<td class="api-documentation">
@if (api.Documentation != null)
{
<p>@api.Documentation</p>
}
else
{
<p>No documentation available.</p>
}
</td>
</tr>
}
This should do the trick!
这篇关于我如何获得的ASP.NET Web API与版本控制和帮助页面的扩展工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!