使用带有文档配置的aspnet-api版本库时出错

时间:2018-04-27 12:47:40

标签: 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 }
        );
    }
}
}

我收到此错误消息:

  

错误CS1061'HttpConfiguration'不包含。的定义   'AddVersionedApiExplorer'并没有扩展方法   'AddVersionedApiExplorer'接受第一个类型的参数   可以找到'HttpConfiguration'(你错过了一个using指令吗?   或汇编参考?)

  

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

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

1 个答案:

答案 0 :(得分:0)

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