Swagger UI使用点表示法显示asp.net webapi参数名称

Muk*_*thi 2 c# swagger swagger-ui asp.net-web-api2 swashbuckle

我为我的asp.net webapi配置了Swagger,类似于下面显示的一个

[HttpGet]
[Route("search")]
public async Task<HttpResponseMessage> Get([FromUri]SearchCriteria searchCriteria)
Run Code Online (Sandbox Code Playgroud)

当我看到webapi的swagger文档时,参数显示为

searchCriteria.sortField searchCriteria.sortDirection等等...作为sortField,sortDirection是SearchCriteria的属性

在此输入图像描述

如何在没有object.propertyname格式的情况下获取参数名称?

任何人都可以帮忙解决这个问题吗?谢谢

ven*_*rik 6

这是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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

SwaggerConfig像这样添加给你:

GlobalConfiguration.Configuration
    .EnableSwagger(c =>
        {
            // other settings omitted
            c.OperationFilter<ParameterFilter>();    
        }); 
Run Code Online (Sandbox Code Playgroud)

顺便说一句:正则表达式的灵感来自/sf/answers/545588991/