对于一个较旧的网络框架项目,我使用Swashbuckle 5.6.0. 当我使用一个类作为get参数时,会发生一件奇怪的事情.它将变量名添加到参数中.

这意味着API的消费者必须添加"记录".放在每个论点的前面.真的很不方便.

对这种行为的解释是什么?

Here is what it looks like in swagger

The definition in the controller:

这是POCO班级:

  public class AssetTransactionAllAssetsSearchRecord2
    {
        public AssetTransactionAllAssetsSearchRecord2() { }
        
        public long Id { get; set; }
        public DateTime FromCreated { get; set; }
        public DateTime ToCreated { get; set; }
        public long? DocumentId { get; set; }

        public long AssetId { get; set; }
        public long HostingAssetId { get; set; }
        public long SystemAssetId { get; set; }
        public long CustomerAssetId { get; set; }

        public string AssetCode { get; set; }
        public string HostingAssetCode { get; set; }
        public string SystemAssetCode { get; set; }
        public string CustomerAssetCode { get; set; }

        public string AssetName { get; set; }
        public string HostingAssetName { get; set; }
        public string SystemAssetName { get; set; }
        public string CustomerAssetName { get; set; }

    }

以下是Swagger配置:

using System.Web.Http;
using WebActivatorEx;
using VI_Web;
using Swashbuckle.Application;
using Swashbuckle.Swagger;
using System.Collections.Generic;
using System.Web.Http.Description;

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

namespace VI_Web
{
    class AuthTokenOperation : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            swaggerDoc.paths.Add("/token", new PathItem
            {
                post = new Operation
                {
                    tags = new List<string> { "UserAuth" },
                    consumes = new List<string>
                {
                    "application/x-www-form-urlencoded"
                },
                    parameters = new List<Parameter> {
                    new Parameter
                    {
                        type = "string",
                        name = "grant_type",
                        required = true,
                        @in = "formData",
                        @default = "password"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "client_id",
                        required = true,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "client_secret",
                        required = true,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "database",
                        required = true,
                        @in = "formData"
                    }

                }
                }
            });
        }
    }

    public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {
                        c.SingleApiVersion("v1", "VI_Web");
                        c.ApiKey("Token")
                        .Description("Filling bearer token here")
                        .Name("Authorization")
                        .In("header");
                        c.DocumentFilter<AuthTokenOperation>();
                    })
                .EnableSwaggerUi(c =>
                    {
                        c.EnableApiKeySupport("Authorization", "header");
                    });
        }
    }
}

推荐答案

我认为这不是华而不实的问题(假设查询终结点确实有效),而是您的ASP.NET安装程序如何处理作为参数传递的"Complex"类型的GET请求处理程序(record).您可以try 将该类型展平为单独的端点参数:

... GetWithAssets(long id, DateTime fromCreated, ...)

或者,也可以使用Parameter Binding in ASP.NET Web API个文档中提到的FromUriAttribute,基于该示例应该将不带参数名称前缀的类型展平:

... GetWithAssets([FromUri] AssetTransactionAllAssetsSearchRecord2 record)

您还可以将您的端点转换为POST并接受JSON Body,这对于类似搜索的端点来说是非常流行的方法.

Csharp相关问答推荐

使用ElasticsearchClient设置忽略属性.默认MappingFor<>

MongoDB将JS查询转换为C#的问题

C#自定义字典与JSON(de—)serialize

在. net毛伊岛窗口的深度链接已经创建""

FileStream. FlushAsync()是否确保文件被写入磁盘?

在多对多关系上不删除实体

不带身份的Blazor服务器.Net 8 Cookie身份验证

从c#列表中删除额外的对象&对象&>从ASP.NET WebForm返回json响应

Mongo作为.NET中Testcontainers的副本集

如何在C#中从正则表达式中匹配一些数字但排除一些常量(也是数字)

WeakReference未被垃圾收集

使用HttpResponseMessage中的嵌套列表初始化JSON

异步实体框架核心查询引发InvalidOperation异常

在swagger示例中添加默认数组列表

使用System.Text.Json进行序列化时发生StackOverflow异常

WinUI 3中DoubleCollection崩溃应用程序类型的依赖属性

如何从Entity Framework Core中填充ListIInterface

如何从SignalR获取连接客户端的域

C#USB条形码 scanner 在第二次扫描时未写入行尾字符

使用ITfoxtec.Identity.Saml2解析相同键多值SAML 2声明