我正在尝试在我的核心 api 项目中使用 Microsoft.AspNetCore.Mvc.Versioning, Version=3.1.0.0。
下载了nuget包,下面是我的代码
statup.cs 文件
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" };
}
}
现在,当我尝试从邮递员那里获取请求时,我正在获取值。
根据我的理解,它不应该提供响应,因为我没有为此证明任何标题值。
请建议我做错了。
更新 1
当我删除这条线
o.ApiVersionSelector = new CurrentImplementationApiVersionSelector(o);
它工作正常。