You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 08:57:02