如何使ASP.NET Web API与版本控制和帮助页面扩展一起使用?

mor*_*are 21 c# asp.net-mvc asp.net-web-api

我已经在我的WebAPI应用程序中实现了一个版本控制框架,并且非常希望能够使用Microsoft的新帮助页面扩展.

Microsoft.AspNet.WebApi.HelpPage

SDammann.WebApi.Versioning

很简单,我不知道如何让他们两个一起工作.我有2个项目:

  • AdventureWorks.Api(主要主机/根应用程序)
  • AdventureWorks.Api.v1(包含API的第一个版本的类库)

版本控制按预期工作.

我已经尝试在根应用程序上安装HelpPage包,当我浏览到帮助页面时,似乎没有找到任何控制器.在内部,我相信它使用:

Configuration.Services.GetApiExplorer().ApiDescriptions
Run Code Online (Sandbox Code Playgroud)

这没有返回结果,所以我收到一个错误.

任何人都可以协助我让这两个包一起工作吗?

编辑: 在开始时,我不确定这是一个路由问题,但最近的评论似乎暗示.这是我的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 }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 11

您需要从项目AdventureWorks.Api.v1项目中获取文档XML文件,并将其放在AdventureWorks.Api项目的bin文件夹中:

然后将这些行添加到Application_Start方法:

// 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"));
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用文档查看API.

有时版本号无法正确拾取,并被???取代

要修复此添加:

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);
    }
Run Code Online (Sandbox Code Playgroud)

准确到这个地方你的ApiGroup.cshtml:

@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>
}
Run Code Online (Sandbox Code Playgroud)

这应该做的伎俩!

  • 这是一个很好的解决方案 - 我认为不可能做到!感谢您抽出宝贵时间来研究这个问题. (2认同)