c# - 使用带有文档配置的 aspnet-api-versioning 库时出错

标签 c# asp.net-web-api documentation versioning

我在我的 Web API 项目中使用 aspnet-api-versioning 库。我按照https://github.com/Microsoft/aspnet-api-versioning/wiki/API-Documentation的指示进行操作。我按照 WebApiConfig.cs 中的说明添加了代码

所以

namespace Nppg.WebApi
{

using Microsoft.Web.Http.Versioning;
using Nppg.WebApi.ActionFilters;
using System.Web.Http;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Add specific Json converter/formetters
        var jsonFormatter = config.Formatters.JsonFormatter;
        jsonFormatter.SerializerSettings.Converters.Add(new LinkDtoConverter());

        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
        config.Filters.Add(new HttpLoggingFilterAttribute());
        config.MessageHandlers.Add(new LogRequestAndResponseHandler());

        #region API versioning configuration
        // allow a client to call you without specifying an api version
        // since we haven't configured it otherwise, the assumed api version will be 1.0
        // https://github.com/Microsoft/aspnet-api-versioning/wiki/New-Services-Quick-Start
        // added to the web api configuration in the application setup
        config.AddApiVersioning(options =>
        {
            options.ApiVersionReader = new MediaTypeApiVersionReader();
            options.AssumeDefaultVersionWhenUnspecified = true;
            options.ApiVersionSelector = new CurrentImplementationApiVersionSelector(options);
        });

        //This code must be used to register "apiVersion" as a contraint
        //var constraintResolver = new DefaultInlineConstraintResolver()
        //{
        //    ConstraintMap = { ["apiVersion"] = typeof(ApiVersionRouteConstraint) }
        //};

        // format the version as "'v'major[.minor][-status]"
        var apiExplorer = config.AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV");

        config.EnableSwagger(
            "{apiVersion}/swagger",
            swagger =>
            {
                swagger.MultipleApiVersions(
                    (apiDescription, version) => apiDescription.GetGroupName() == version,
                    info =>
                    {
                        foreach (var group in apiExplorer.ApiDescriptions)
                        {
                            info.Version(group.Name, $"Sample API {group.ApiVersion}");
                        }
                    });
            })
         .EnableSwaggerUi(swagger => swagger.EnableDiscoveryUrlSelector());



        #endregion

        // Web API routes
        //config.MapHttpAttributeRoutes(constraintResolver); // With URL Path Versioning
        config.MapHttpAttributeRoutes(); // Whithout URL Path Versioning

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
}

我收到此错误消息:

Error CS1061 'HttpConfiguration' does not contain a definition for 'AddVersionedApiExplorer' and no extension method 'AddVersionedApiExplorer' accepting a first argument of type 'HttpConfiguration' could be found (are you missing a using directive or an assembly reference?)

Error CS1061 'HttpConfiguration' does not contain a definition for 'EnableSwagger' and no extension method 'EnableSwagger' accepting a first argument of type 'HttpConfiguration' could be found (are you missing a using directive or an assembly reference?)

知道为什么我收到这些消息吗?

最佳答案

尝试在 Here 安装 Microsoft.AspNet.WebApi.Versioning.ApiExplorer 的 nuget 包

关于c# - 使用带有文档配置的 aspnet-api-versioning 库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50062750/

相关文章:

c# - 如何使用 SQL Server 数据库部署 C#/WPF 应用程序

c# - 动态 PropertyGrid 属性

c# - 文档注释(子类)

java - 文档 - 它是一个模板吗?

c# - 让 svcutil 从 C# 文件中获取文档?

c# - 重构LINQ to Entities查询

c# - 为多个 Outlook 版本的插件制作单个安装程序

c# - 如何在与 postman 的同一请求中测试上传文件和json?

asp.net-mvc-4 - 如何在 MVC 4 的请求中获取查询字符串变量?

c# - 如何为 Web API 2 异步操作中的异常获取正确的堆栈跟踪?