Swagger UI以点表示法显示asp.net webapi参数名称 [英] Swagger UI displaying the asp.net webapi parameter name with dot notation

查看:170
本文介绍了Swagger UI以点表示法显示asp.net webapi参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为asp.net webapi配置了Swagger,类似于以下所示

I have configured Swagger for my asp.net webapi which is similar to one shown below

[HttpGet]
[Route("search")]
public async Task<HttpResponseMessage> Get([FromUri]SearchCriteria searchCriteria)

当我看到有关webapi的详尽文档时,该参数显示为

When i see the swagger documentation for the webapi , the parameter is displaying as

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

searchCriteria.sortField searchCriteria.sortDirection and so on... being the sortField, sortDirection are properties of SearchCriteria

如何获取不带object.propertyname格式的参数名称?

How to get the parameter names without the object.propertyname format?

任何人都可以帮助解决此问题吗? 谢谢

Can anyone help how to fix this? Thanks

推荐答案

这是我曾经用来从查询参数中删除类名称的OperationFilter.

Here's an OperationFilter I once used to remove the class name from query parameters.

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>();    
        }); 

顺便说一句:regex受到 https://stackoverflow.com/a/7794128/502395

BTW: The regex is inspired by https://stackoverflow.com/a/7794128/502395

这篇关于Swagger UI以点表示法显示asp.net webapi参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆