ibe*_*dev 6 swagger swagger-ui asp.net-core-mvc api-versioning asp.net-core
希望有人尝试过与MVC 6和Swagger中的版本化API类似的东西,以显示有关不同版本的文档.
我在这个ASP.NET 5存储库中使用MVC 6中推荐的API版本.我所做的唯一改变是GetVersion方法从请求的自定义http头读取api版本:
//in VersionRangeValidator.cs
public static string GetVersion(HttpRequest request)
{
//return request.Query["version"];
if (!string.IsNullOrWhiteSpace(request.Headers[Constants.CommonRoutingDefinitions.ApiVersionSegmentName]))
{
return request.Headers[Constants.CommonRoutingDefinitions.ApiVersionSegmentName];
}
return Constants.CommonRoutingDefinitions.CurrentApiVersion;
}
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的控制器:
[Route("api/[controller]")]
[Produces(Constants.MediaTypeNames.ApplicationJson)]
public class TagsController : Controller
{
private readonly ITagService _tagService;
public TagsController(ITagService tagService)
{
_tagService = tagService;
}
/// <summary>
/// Version 1 by default
/// </summary>
/// <returns>All the tags</returns>
[HttpGet]
[Produces(typeof(IEnumerable<Tag>))]
public IEnumerable<Tag> GetTags()
{
IEnumerable<Tag> tags = _tagService.GetTags();
return tags;
}
/// <summary>
/// Version 2
/// </summary>
/// <returns>All the tags V2</returns>
[VersionGet("", versionRange: "[2]")]
public IEnumerable<Tag> GetTagsV2()
{
IList<Tag> tags = new List<Tag>
{
new Tag { Id = 1, Links = Enumerable.Empty<Link>().ToList(), Name = "Tag version 2" }
};
return tags;
}
}
Run Code Online (Sandbox Code Playgroud)
使用自定义http标头进行版本控制即可
GET/api /标签
Content-Type:application/json
默认情况下将触发GetTags()操作,因为没有指定标头和
GET/api /标签
api-version:2
Content-Type:application/json
将点击GetTagsV2()动作.
我已按照此博客中的步骤添加了Swagger UI和Swagger GEN库,因此在我的project.json以下依赖项:
"Swashbuckle.SwaggerGen": "6.0.0-rc1-final",
"Swashbuckle.SwaggerUi": "6.0.0-rc1-final"
Run Code Online (Sandbox Code Playgroud)
然后在我的Startup.cs中,我将Swagger添加到管道中
//inside Configure(IApplicationBuilder app)
app.UseSwaggerGen();
app.UseSwaggerUi();
Run Code Online (Sandbox Code Playgroud)
我按如下方式配置Swagger:
private void ConfigureSwagger(IServiceCollection services)
{
services.AddSwaggerGen();
services.ConfigureSwaggerDocument(options =>
{
options.MultipleApiVersions(new Swashbuckle.SwaggerGen.Info[]
{
new Swashbuckle.SwaggerGen.Info
{
Version = "v1",
Title = "MyApp API",
Description = "A RESTful API"
},
new Swashbuckle.SwaggerGen.Info
{
Version = "v2",
Title = "MyApp API (v2)",
Description = "A RESTful API"
}
}, (description, version) => {
//description is an instance of ApiDescription and
//version is either "v1" or "v2"
//depending on the user choice in swagger UI page
//TODO, how can I know whether the action belongs to v1 or to v2 to return true or false as appropriate?
});
options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(Configuration["Documentation:SwaggerDocXml"]));
});
services.ConfigureSwaggerSchema(options =>
{
options.DescribeAllEnumsAsStrings = true;
options.ModelFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlTypeComments(Configuration["Documentation:SwaggerDocXml"]));
});
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何从描述(这是Microsoft.AspNet.Mvc.ApiExplorer.ApiDescription的一个实例)获得必要的信息,以了解是否必须在Swagger UI中显示给定的操作或不依赖在指定的版本上.任何提示将不胜感激.这将有助于理解这个用于版本控制的ASP.NET 5存储库实现是如何工作的,因为我仍然不能很好地理解它,并且无法找到关于动作约束如何工作的良好解释.
PS:这个stackoverflow问题帮助我实现了MVC 6的版本控制,但我找不到很多关于Swagger如何与这种版本化API的方式集成.
小智 2
我在这里第一次回答问题,希望你觉得有用。对于代码片段的格式错误,我深表歉意,但我现在没有时间。
您几乎就在那里,但您没有在描述中添加版本过滤器。这在我的实现中适用于以下 url 格式:
"/v1/Sessions" and "/v3/Sessions"
options.MultipleApiVersions(new Info[]{
new Info
{
Version = "v1",
Title = "your_own",
Description = "Defines the API to access... your_own",
TermsOfService = "your_own",
Contact = new Contact()
{
Email = "your_own",
Name = "your_own"
}
},
new Info
{
Version = "v3",
Title = "your_own",
Description =
"Defines the API to .... your_own",
TermsOfService = "your_own",
Contact = new Contact()
{
Email = "your_own",
Name = "your_own"
}
}}, (description, version) => {
//Here we compare if the version is part of the incoming URI, if yes, show it on swagger page.
return description.RelativePath.ToLower().Contains(version.ToLower()); } );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4991 次 |
| 最近记录: |