【问题标题】:Swashbuckle: Customize endpoint path (AspNetCore)Swashbuckle:自定义端点路径 (AspNetCore)
【发布时间】:2020-08-22 17:55:43
【问题描述】:

我有这个设置,我想组织由同一应用程序提供的多个 API . 对于每个 api,我从子域获取前缀并将其重定向到应用程序服务器,例如:

user.api.example.com/v1/profile --> api.example.com/user/v1/profile

admin.api.example.com/v1/companies --> api.example.com/admin/v1/companies

使用此设置,我需要在生成 swagger json 文件时删除路径前缀(“/user”、“/admin”)。

是否可以在生成 json 文件之前配置一个函数来操作每个端点的路径?

我只想更改进入 swagger json 文件的内容,而不是实际的端点路径!

【问题讨论】:

    标签: swashbuckle swashbuckle.aspnetcore


    【解决方案1】:

    文档过滤器可以满足这种特殊需求:

    public class PathPrefixDocumentFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var editedPaths = new OpenApiPaths();
            foreach(var kv in swaggerDoc.Paths)
            {
                var newKey = string.Join("/", kv.Key.Split('/').Skip(2));
                editedPaths.Add("/"+newKey, kv.Value);
            }
            swaggerDoc.Paths = editedPaths;
            var a = swaggerDoc.Paths;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多