在我的web项目中,我正在使用aspnet版本库。我遵循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‘不包含'AddVersionedApiExplorer’的定义,也找不到接受'HttpConfiguration‘类型的第一个参数的扩展方法'AddVersionedApiExplorer’(您缺少使用指令还是程序集引用?)
和
Error CS1061 'HttpConfiguration‘不包含'EnableSwagger’的定义,也找不到接受'HttpConfiguration‘类型的第一个参数的扩展方法'EnableSwagger’(您缺少使用指令还是程序集引用?)
知道我为什么会收到这些信息吗?
发布于 2018-09-27 20:09:16
尝试在Microsoft.AspNet.WebApi.Versioning.ApiExplorer的这里上安装nuget软件包
https://stackoverflow.com/questions/50062750
复制相似问题