swashbuckle - Swashbuckle:自定义端点路径 (AspNetCore)
问题描述
我有这个设置,我想组织由同一个应用程序提供的多个 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 文件的内容,而不是实际的端点路径!
解决方案
文档过滤器是满足这种特殊需求的答案:
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;
}
}
推荐阅读
- debugging - 我们如何限制 GDB 仅调试特定源(例如 yacc/lex)以获取 step + next 指令?
- r - 在data.table R中以编程方式将不同的函数应用于不同的列
- python - 函数式编程,如何从单个迭代器一次有效地构建多个列表?
- node.js - 安装 Angular 的问题
- python - 使 saxon-c 在 Python 中可用
- python - 在网络抓取方面需要帮助
- excel - 为什么我不能像这样保存我的文件?Excel/VBA
- python - 带有 Gstreamer 管道的 Kinesis 直播流作为带有 sudo 的 python 子进程:“没有这样的元素或插件‘kvssink’”
- r - 如何在 Power BI 折线图中绘制平滑线?
- sql - 在 Visual Basic 中为 MSAccess 链接 Sql 命令