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

Azure Functions路由冲突:同路径不同参数触发错误函数

解决Azure Functions路由匹配异常问题

问题原因

你遇到的问题是因为GetAsset函数使用了{*guid}这种catch-all通配符路由,它会匹配所有以api/cdp/开头的路径,包括api/cdp(此时guid被解析为空字符串),从而抢占了GetAssetList的精确路由匹配。

解决方案

方案1:修改GetAsset的路由为非通配符(适合guid无斜杠场景)

将GetAsset的路由从api/cdp/{*guid}改为api/cdp/{guid},这样该路由仅匹配带有非空guid的路径,api/cdp会优先匹配GetAssetList的精确路由:

[Function("Asset")]
public async Task<IActionResult> GetAsset([HttpTrigger(AuthorizationLevel.Function, "get", Route = "api/cdp/{guid}")] HttpRequest req, string guid)
{
    _logger.LogInformation("C# HTTP trigger function processed a request.");
    return new OkObjectResult("asset");
}

方案2:给通配符路由添加长度约束(需保留带斜杠的guid场景)

如果需要支持包含斜杠的guid(比如cdp/abc/def),可以给{*guid}添加minlength(1)约束,确保guid至少有1个字符,避免匹配空路径:

[Function("Asset")]
public async Task<IActionResult> GetAsset([HttpTrigger(AuthorizationLevel.Function, "get", Route = "api/cdp/{*guid:minlength(1)}")] HttpRequest req, string guid)
{
    _logger.LogInformation("C# HTTP trigger function processed a request.");
    return new OkObjectResult("asset");
}

验证修改

修改后重新运行函数:

  • 调用http://localhost:7075/interKonnect/cdp会触发GetAssetList函数
  • 调用http://localhost:7075/interKonnect/cdp/8933会触发GetAsset函数

内容的提问来源于stack exchange,提问作者Karthick J

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 12:40:56