![](/img/trans.png)
[英]Can an ASP.NET WebAPI action parameter be excluded from Swashbuckle/Swagger UI?
[英]Swagger UI displaying the asp.net webapi parameter name with dot notation
我已经为我的asp.net webapi配置了Swagger,类似于以下所示
[HttpGet]
[Route("search")]
public async Task<HttpResponseMessage> Get([FromUri]SearchCriteria searchCriteria)
当我看到有关webapi的详细文档时,该参数显示为
searchCriteria.sortField searchCriteria.sortDirection等等...作为sortField,sortDirection是SearchCriteria的属性
如何不使用object.propertyname格式获取参数名称?
谁能帮忙解决此问题? 谢谢
这是我曾经用来从查询参数中删除类名称的OperationFilter
。
public class ParameterFilter : IOperationFilter
{
private const string Pattern = @"^ # Match start of string
.*? # Lazily match any character, trying to stop when the next condition becomes true
\. # Match the dot";
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.parameters == null)
{
return;
}
foreach (var parameter in operation.parameters
.Where(x => x.@in == "query" && x.name.Contains(".")))
{
parameter.name = Regex.Replace(
parameter.name,
Pattern,
string.Empty,
RegexOptions.IgnorePatternWhitespace);
}
}
}
像这样将其添加到您的SwaggerConfig
:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
// other settings omitted
c.OperationFilter<ParameterFilter>();
});
顺便说一句:正则表达式的灵感来自https://stackoverflow.com/a/7794128/502395
我假设您正在使用Swashbuckle。
查看DocumentFilters
和OperationFilters
。 您可以扩展Swashbuckle以在文档或操作级别进行干预以修改输出。
通读Swashbuckle文档 ,很容易实现这两个接口。
当您将参数Name = ""
传递给FormUri
属性时,Swashbuckle会以非限定形式生成参数名称,例如sortField
而不是searchCriteria.sortField
。
public async Task<HttpResponseMessage> Get([FromUri(Name = "")]SearchCriteria searchCriteria)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.