C# Microsoft.AspNetCore.Mvc.Versioning,版本=3.1.0.0在.net core 2.2中不工作
我正在尝试在我的核心api项目中使用Microsoft.AspNetCore.Mvc.Versioning,Version=3.1.0.0 下载了nuget软件包,下面是我的代码 statup.cs文件C# Microsoft.AspNetCore.Mvc.Versioning,版本=3.1.0.0在.net core 2.2中不工作,c#,asp.net-core,aspnet-api-versioning,C#,Asp.net Core,Aspnet Api Versioning,我正在尝试在我的核心api项目中使用Microsoft.AspNetCore.Mvc.Versioning,Version=3.1.0.0 下载了nuget软件包,下面是我的代码 statup.cs文件 public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguratio
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
//services.AddApiVersioning();
services.AddApiVersioning
(o =>
{
//o.AssumeDefaultVersionWhenUnspecified = true ;
//o.DefaultApiVersion = new ApiVersion(new DateTime(2016, 7, 1));
o.ReportApiVersions = true;
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new ApiVersion(1, 0);
o.ApiVersionReader = new HeaderApiVersionReader("api-version");
o.ApiVersionSelector = new CurrentImplementationApiVersionSelector(o);
}
);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseApiVersioning();
app.UseMvc();
}
}
和如下所示的控制器值
[ApiVersion( "2.0" )]
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
}
它工作正常。
CurrentImplementationApiVersionSelector选择最大API
没有版本状态的可用版本。如果没有对手
找到后,它将返回到配置的DefaultApiVersion。[……]
如果您没有提供任何调用该特定端点的api版本,它将找到最大版本(在您的案例中为2.0)并将其用作默认版本。这就是调用该方法的原因
[…]例如,如果版本“1.0”、“2.0”和“3.0-Alpha”是
如果可用,则将选择“2.0”,因为它是最高的,
已实施或发布的API版本
这是99%正确。它在没有指定任何标题的情况下工作的原因是设置了
options.AssumeDefaultVersionWhenUnspecified=true
。选择器解析所述的最大版本。我不确定我们是否掌握了全部情况,但假设我们掌握了,那么您应该会收到400,因为没有1.0
的实现。
o.ApiVersionSelector = new CurrentImplementationApiVersionSelector(o);