[英]Swagger UI displaying the asp.net webapi parameter name with dot notation
I have configured Swagger for my asp.net webapi which is similar to one shown below 我已经为我的asp.net webapi配置了Swagger,类似于以下所示
[HttpGet]
[Route("search")]
public async Task<HttpResponseMessage> Get([FromUri]SearchCriteria searchCriteria)
When i see the swagger documentation for the webapi , the parameter is displaying as 当我看到有关webapi的详细文档时,该参数显示为
searchCriteria.sortField searchCriteria.sortDirection and so on... being the sortField, sortDirection are properties of SearchCriteria searchCriteria.sortField searchCriteria.sortDirection等等...作为sortField,sortDirection是SearchCriteria的属性
How to get the parameter names without the object.propertyname format? 如何不使用object.propertyname格式获取参数名称?
Can anyone help how to fix this? 谁能帮忙解决此问题? Thanks
谢谢
Here's an OperationFilter
I once used to remove the class name from query parameters. 这是我曾经用来从查询参数中删除类名称的
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);
}
}
}
Add it to you SwaggerConfig
like this: 像这样将其添加到您的
SwaggerConfig
:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
// other settings omitted
c.OperationFilter<ParameterFilter>();
});
BTW: The regex is inspired by https://stackoverflow.com/a/7794128/502395 顺便说一句:正则表达式的灵感来自https://stackoverflow.com/a/7794128/502395
I assume you are using Swashbuckle. 我假设您正在使用Swashbuckle。
Look at DocumentFilters
and OperationFilters
. 查看
DocumentFilters
和OperationFilters
。 You can extend Swashbuckle to intervene at the document or operation level to modify the output. 您可以扩展Swashbuckle以在文档或操作级别进行干预以修改输出。
Read through the Swashbuckle documentation , it is fairly straight forward to implement either of these two interfaces. 通读Swashbuckle文档 ,很容易实现这两个接口。
当您将参数Name = ""
传递给FormUri
属性时,Swashbuckle会以非限定形式生成参数名称,例如sortField
而不是searchCriteria.sortField
。
public async Task<HttpResponseMessage> Get([FromUri(Name = "")]SearchCriteria searchCriteria)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.