gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:22 28 4
gpt4 key购买 nike

我在我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com