How do I get ASP.NET Web API working with versioning and the Help Page extension?

前端 未结 3 1175
北恋
北恋 2021-01-31 10:28

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.

Microsof

3条回答
  •  星月不相逢
    2021-01-31 10:36

    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);
        }
        
            @api.HttpMethod.Method @api.RelativePath
            
            @if (api.Documentation != null)
            {
                

    @api.Documentation

    } else {

    No documentation available.

    } }

    This should do the trick!

提交回复
热议问题