如何解决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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。