1、管理 NuGet 程序包
Microsoft.AspNetCore.Mvc.Versioning 【注意版本号:4.0.0-preview8.19405.7】
Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer 【注意版本号:4.0.0-preview8.19405.7】
如果版本安装错了,运行程序会报错,未经处理的异常
*
2、Startup.cs(ConfigureServices)
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(options =>
{
options.UseCentralRoutePrefix(new RouteAttribute("api/v1/[controller]"));
});
services.AddApiVersioning(options =>
{
options.ReportApiVersions = true; //可选配置,设置为true时,header返回版本信息
options.ApiVersionReader = new HeaderApiVersionReader("api-version");//HTTP Header报头传递
options.AssumeDefaultVersionWhenUnspecified = true;//是否启用未指明版本API,指向默认版本
options.DefaultApiVersion = new ApiVersion(1, 0);
})
.AddVersionedApiExplorer(option =>
{
option.GroupNameFormat = "'v'VVVV";//api组名格式
option.AssumeDefaultVersionWhenUnspecified = true;//是否提供API版本服务
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
}
3、Startup.cs(Configure)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseApiVersioning();
}
*
4、***Controller.cs
使用 ApiVersion 定义要支持 API 版本控制的控制器(controller)(多个版本),使用 MapToApiVersion 定义特定 Action 版本号
请求样式1:https://localhost:44382/api/v3/Default
[ApiVersion("3.0")]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
public class DefaultController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
请求样式2:https://localhost:44382/api/Default?api-version=3.0
[ApiVersion("3.0")]
[ApiVersion("2.0")]
[ApiVersion("1.0")]
[Route("api/[controller]")]
[ApiController]
public class DefaultController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
[MapToApiVersion("7.0")]
[MapToApiVersion("6.0")]
[MapToApiVersion("5.0")]
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
[ApiVersionNeutral]
[HttpGet("{id}")]
public IEnumerable<WeatherForecast> Get(int id)
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
*
*
*
*
*
5、在 URL 中追加 api-version=2
*
*
6、通过HTTP Headers来实现版本控制
*
*
*
*
7、参数特性
ApiVersionNeutral:不指定Api版本号;随便访问;
MapToApiVersion:支持多个版本的单控制器;
Deprecated:弃用,废弃;标记过时的Api为弃用状态;用Deprecated修饰控制器;仍然可以调用该版本,只是一种让 调用API 用户意识到以下版本在将来会被弃用。
*
*
*
*
*
*