我在我的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 }
        );
    }
}
}


我收到此错误消息:


  错误CS1061'HttpConfiguration'不包含以下定义
  'AddVersionedApiExplorer',没有扩展方法
  'AddVersionedApiExplorer'接受类型的第一个参数
  可以找到“ HttpConfiguration”(您是否缺少using指令
  或装配参考?)





  错误CS1061'HttpConfiguration'不包含以下定义
  'EnableSwagger'并且没有扩展方法'EnableSwagger'接受
  可以找到类型为“ HttpConfiguration”的第一个参数(您是
  缺少using指令或程序集引用?)


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

最佳答案

尝试在Here处为Microsoft.AspNet.WebApi.Versioning.ApiExplorer安装nuget软件包。

09-19 14:37