C# Swashback swagger.json大于4 mb净内核

C# Swashback swagger.json大于4 mb净内核,c#,swagger,asp.net-core-webapi,swashbuckle,C#,Swagger,Asp.net Core Webapi,Swashbuckle,我的api端点变得太大,我需要最小化它或将其划分为多个swagger.json文件。 我想将swagger.json文件上传到power Automation,但有两条规则。每个文件最多4 Mb,最多256个函数。我不符合这些要求 我希望每个控制器/组有一个招摇过市的文件,这将最大限度地减少函数数量并减小文件大小。 但我不知道如何配置(Swashback),或者应该使用documentFilters进行配置? 我已经使用了APVersioning来减少一些函数和大小,但这还不够。我无法更改完整的

我的api端点变得太大,我需要最小化它或将其划分为多个swagger.json文件。 我想将swagger.json文件上传到power Automation,但有两条规则。每个文件最多4 Mb,最多256个函数。我不符合这些要求

我希望每个控制器/组有一个招摇过市的文件,这将最大限度地减少函数数量并减小文件大小。 但我不知道如何配置(Swashback),或者应该使用documentFilters进行配置?


我已经使用了APVersioning来减少一些函数和大小,但这还不够。我无法更改完整的url端点。我只需要多个文件而不仅仅是版本。

有两个选项可以实现这一点。但是我想在不改变现有api的任何URL的情况下实现它

  • 在每个控制器中,您可以添加并设置Apiversion
    [Apiversion(“2.0”)]
    ,然后设置控制器名称,即
    [Apiversion(“2.0.order”)]
    。每个控制器都有一个版本。 此解决方案将更改URL,并且现有api无法访问。

  • 另一个解决方案是使用过滤器为每个操作创建标记,现在我们可以为每个端点创建过滤器

  • 然后应用文档过滤器

     public class SwaggerDocumentFilter : IDocumentFilter
        {
            public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
            {
                // Key is read-only so make a copy of the Paths property
                var pathsFiltered = new OpenApiPaths();
                var array = context.DocumentName.Split("-");
                string version = array[0];
                string tag = string.Empty;
                if (array.Count() > 1)
                {
                    tag = array[1];
                }
    
                foreach (var path in swaggerDoc.Paths)
                {
                    if (path.Value.Operations.Values.First().Tags.FirstOrDefault(c => c.Name == version) != null)
                    {
                        if (path.Value.Operations.Values.First().Tags.FirstOrDefault(c => c.Name.ToLower() == tag.ToLower()) != null ||
                            tag == string.Empty)
                        {
                            // Add the path to the filtered collection
                            pathsFiltered.Add(path.Key, path.Value);
                        }
                    }
                }
                swaggerDoc.Paths = pathsFiltered;
            }
        }
    
    关键是要有匹配的c.SwaggerEndpoint(在UseSwaggerUI中)和options.SwaggerDoc(在SwaggerGenOptions中)

    要检查的一个好例子是。
    希望这对任何人都有帮助。

    我想你可以使用“组”,但我忘记了具体细节。也许可以尝试将其作为搜索的一部分terms@pinkfloydx33谢谢,我用的是群名。如果我改变了这个设置,url就会改变。i、 e localhost/api/v1/orders,并将其更改为localhost/api/v1.orders/orders。所以小组是正确的方法,我将在阅读代码后更新问题:执行
    FirstOrDefault()。任何
    都可能导致NullReferenceException。要么做一个
    First()。然后你会得到一个更好的例外。但希望这不会发生,因为标签是自动添加的。
    
     public class SwaggerDocumentFilter : IDocumentFilter
        {
            public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
            {
                // Key is read-only so make a copy of the Paths property
                var pathsFiltered = new OpenApiPaths();
                var array = context.DocumentName.Split("-");
                string version = array[0];
                string tag = string.Empty;
                if (array.Count() > 1)
                {
                    tag = array[1];
                }
    
                foreach (var path in swaggerDoc.Paths)
                {
                    if (path.Value.Operations.Values.First().Tags.FirstOrDefault(c => c.Name == version) != null)
                    {
                        if (path.Value.Operations.Values.First().Tags.FirstOrDefault(c => c.Name.ToLower() == tag.ToLower()) != null ||
                            tag == string.Empty)
                        {
                            // Add the path to the filtered collection
                            pathsFiltered.Add(path.Key, path.Value);
                        }
                    }
                }
                swaggerDoc.Paths = pathsFiltered;
            }
        }