问题描述
在使用属性路由时是否可以在 Swagger UI/Swashbuckle 中利用 MultipleApiVersions?
Is it possible to leverage MultipleApiVersions in Swagger UI / Swashbuckle when using attribute routing?
具体来说,我通过以下方式实现版本控制:
Specifically, I implemented versioning by:
using System.Web.Http;
namespace RESTServices.Controllers.v1
{
[Route("api/v1/Test")]
public class TestV1Controller : ApiController
{ ... }
版本 2 将位于 v2 命名空间中.在名为 TestV2Controller 的控制器中.该路线将包含 v2.
Version 2 would be in a v2 namespace. In a controller named TestV2Controller. The route would have v2 in it.
是否可以传递一个允许这样做的 lambda?我在网上找到了一个编译的示例 lambda,但随后 Swagger 完全停止工作.无法在浏览器中设置断点或查看 Swagger.
Is it possible to pass a lambda in that will allow this? I found a sample lambda online which compiled, but then Swagger stopped working completely. Couldn't hit breakpoints or see Swagger in the browser.
推荐答案
.EnableSwagger(c => c.MultipleApiVersions(
(apiDesc, version) =>
{
var path = apiDesc.RelativePath.Split('/');
var pathVersion = path[1];
return CultureInfo.InvariantCulture.CompareInfo.IndexOf(pathVersion, version, CompareOptions.IgnoreCase) >= 0;
},
vc =>
{
vc.Version("v2", "Swashbuckle Dummy API V2"); //add this line when v2 is released
// ReSharper disable once ConvertToLambdaExpression
vc.Version("v1", "Swashbuckle Dummy API V1");
}
))
这篇关于通过属性版本控制在 Swagger 中利用 MultipleApiVersions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!