Swashbuckle.AspNetCore升级后如何恢复1.1.0版本一致的operationId
适配方案
直接替换你现有的CustomOperationIds配置为以下实现即可完全匹配1.1.0版本的operationId生成规则:
using System.Text.RegularExpressions; // Swagger服务注册位置添加如下配置 options.CustomOperationIds(apiDesc => { // 第一步:把路由中的{参数名}替换为By[首字母大写的参数名] var processedPath = Regex.Replace(apiDesc.RelativePath, @"\{(\w+)\}", m => $"By{char.ToUpper(m.Groups[1].Value[0])}{m.Groups[1].Value.Substring(1)}"); // 第二步:移除所有路径斜杠,拼接首字母大写的HTTP方法 return processedPath.Replace("/", "") + char.ToUpper(apiDesc.HttpMethod[0]) + apiDesc.HttpMethod.Substring(1).ToLower(); });
规则验证
- 带路径参数路由
/api/Games/{id}+ POST方法:处理后得到ApiGamesByIdPost,完全匹配示例结果 - 无参数路由
/api/Games/Sequence+ GET方法:处理后得到ApiGamesSequenceGet,完全匹配示例结果 - 多路径参数场景例如
/api/Games/{gameId}/Players/{playerId}+ PUT方法:处理后得到ApiGamesByGameIdPlayersByPlayerIdPut,符合旧版本默认生成逻辑
内容的提问来源于stack exchange,提问作者wesleylim1993
相关产品推荐
相关产品推荐

