c# - swagger-ui 中默认的 api 版本值
问题描述
我已经在我们的asp.core wep-api项目中配置了 swagger,它工作得很好。现在我正在寻找解决方案,当swagger-ui出现如下所示
api 版本部分应根据代码端的配置自动填充。
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "My API",
Contact = new Contact
{
Name = "My Api",
Url = "https://109.com/"
}
});
var security = new Dictionary<string, IEnumerable<string>>
{
{"Bearer", new string[] { }},
};
c.AddSecurityDefinition("Bearer", new ApiKeyScheme
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = "header",
Type = "apiKey"
});
c.AddSecurityRequirement(security);
});
解决方案
您需要安装Microsoft.AspNetCore.Mvc.Versioning
和Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer
打包以在 Swagger 中启用 API 版本控制。
您可以在此处查看更多详细信息。
在ConfigureServices
方法中将版本控制方案定义为
services.AddVersionedApiExplorer(o =>
{
o.GroupNameFormat = "'v'VVV";
o.SubstituteApiVersionInUrl = true;
});
services.AddApiVersioning(config =>
{
config.DefaultApiVersion = new ApiVersion(1, 0);
config.AssumeDefaultVersionWhenUnspecified = true;
config.ReportApiVersions = true;
});
推荐阅读
- angular - Mat Select 没有获得选定的值角度 6 TS
- mit-scratch - 如何在 Scratch 3.0 中将服装定位到中心?
- python - 将 rgb 蒙版图像转换为 coco json 多边形格式
- reactjs - 如何对yup中的单个值进行多次验证?
- python - 如何在 python 中找出 Nifti 文件的通道号?
- javascript - 使用 JavaScript 合并 2 个数组并将其导出到 xls 文件
- c++ - 如何在类中创建线程?
- android - 无法从 C:\Program Files (x86)\Microsoft XDE\10.0.10586.0 中的位置打开 xde.exe
- flutter - 在内部使用带有 Cupertino 导航栏的小部件
- wso2 - 使用预打包的 wso2 身份服务器作为 wso2 api 管理器的密钥管理器时无法验证令牌