C# 如何为SwaggerUi中显示的路径提供路径参数?
我已经在我的WebAPI项目上设置了Swagger/Swashback。我遵循了介绍如何使用Swagger设置Aspnet.WebApi.Versioning的。我的API有多个版本,因此在路由属性中设置了一个C# 如何为SwaggerUi中显示的路径提供路径参数?,c#,asp.net-web-api,swagger,swashbuckle,api-versioning,C#,Asp.net Web Api,Swagger,Swashbuckle,Api Versioning,我已经在我的WebAPI项目上设置了Swagger/Swashback。我遵循了介绍如何使用Swagger设置Aspnet.WebApi.Versioning的。我的API有多个版本,因此在路由属性中设置了一个{version}参数,如下所示: [ApiVersion("2")] [RoutePrefix("api/{version:apiVersion}/values")] public class AccountController : ApiController { [Route(
{version}
参数,如下所示:
[ApiVersion("2")]
[RoutePrefix("api/{version:apiVersion}/values")]
public class AccountController : ApiController
{
[Route("UserInfo")]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
我试图简单地替换ApiExplorer
的RelativePath
中的{version}
在UI中起作用,但由于{version}
被更改为查询
参数而不是路径
,这不是我的API配置方式,因此中断了测试功能
在swagger构建文档之前,我是否可以修改ApiExplorer中的值,同时仍然保留测试功能?我使用的是Swashback和文档过滤器
public class VersionedOperationsFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
foreach (var apiDescriptionsGroup in context.ApiDescriptionsGroups.Items)
{
var version = apiDescriptionsGroup.GroupName;
foreach (ApiDescription apiDescription in apiDescriptionsGroup.Items)
{
apiDescription.RelativePath = apiDescription.RelativePath.Replace("{version}", version);
}
}
}
}
并在Startup.cs中的ConfigureServices方法中添加此筛选器:
services.AddMvc();
var defaultApiVer = new ApiVersion(1, 0);
services.AddApiVersioning(option =>
{
option.ReportApiVersions = true;
option.AssumeDefaultVersionWhenUnspecified = true;
option.DefaultApiVersion = defaultApiVer;
});
services.AddMvcCore().AddVersionedApiExplorer(e=>e.DefaultApiVersion = defaultApiVer);
services.AddSwaggerGen(
options =>
{
var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
options.DocumentFilter<VersionedOperationsFilter>();
//// add a swagger document for each discovered API version
//// note: you might choose to skip or document deprecated API versions differently
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName.ToString(),
CreateInfoForApiVersion(description));
}
//// integrate xml comments
options.IncludeXmlComments(Path.ChangeExtension(Assembly.GetEntryAssembly().Location, "xml"));
});
services.AddMvc();
var defaultApiVer=新的ApiVersion(1,0);
services.addapVersioning(选项=>
{
option.ReportApiVersions=true;
option.AssumeDefaultVersionWhenUnspecified=true;
option.DefaultApiVersion=defaultApiVer;
});
services.AddMvcCore().AddVersionedApiExplorer(e=>e.DefaultApiVersion=defaultApiVer);
services.addswagggen(
选项=>
{
var provider=services.BuildServiceProvider().GetRequiredService();
options.DocumentFilter();
////为每个发现的API版本添加一个招摇过市文档
////注意:您可以选择以不同的方式跳过或记录不推荐使用的API版本
foreach(provider.ApiVersionDescriptions中的变量描述)
{
options.SwaggerDoc(description.GroupName.ToString(),
CreateInfoForApiVersion(描述));
}
////集成xml注释
includexmlcoments(Path.ChangeExtension(Assembly.GetEntryAssembly().Location,“xml”);
});
用于API版本控制的API资源管理器现在支持开箱即用的行为,使用:
options.SubstituteApiVersionInUrl = true
这将为您完成替换工作,并从操作描述符中删除API版本参数。通常不需要更改应用于替换值的默认格式,但可以使用以下方法进行更改:
options.SubstitutionFormat = "VVV"; // this is the default
谢谢你的回复,并对迟来的回复表示歉意,因为我刚刚抽出时间再看一次。您的代码看起来很理想,但是用于
IDocumentFilter
的签名与我使用的Seashbuckle版本不匹配。相反,它期望应用(SwagggerDocument SwagggerDoc、SchemaRegistry SchemaRegistry、IApiExplorer apiExplorer)
-请注意,我正在处理的是一个WebApi项目,而不是一个MVC项目,如果它有所不同的话。问题是,我使用的是Swashback.AspNetCore版本,所以它有不同的签名,但原理应该是相同的-就像
options.SubstitutionFormat = "VVV"; // this is the default