Azure API管理与URL模板参数和功能后端



我正在尝试在Azure API Management上设置一个端点,该端点带有路由到Azure函数的url参数。

EG:GET my-api-gateway.azure-api.net.com/product/{productId}

我的入站策略只是设置后端并将模板参数移动到查询参数。

<inbound>
<base />
<set-query-parameter name="productId" exists-action="override">
<value>@(context.Request.MatchedParameters["productId"])</value>
</set-query-parameter>
<set-backend-service id="apim-generated-policy" backend-id="product-function" />
</inbound>

然而,当我调用my-api-gateway.azure-api.net.com/product/123时,/123也被传递到后端函数url

https://my-function.azurewebsites.net/api/product-function/123?productId=123

返回404。

有办法让这个工作吗?

一种解决方法是将路由添加到后端,正如您在自己的答案中所示。然而,如果你不能做到这一点,那么你最初的问题的答案是引入一个uri重写:

<inbound>
<base />
<rewrite-uri template="/product-function" copy-unmatched-params="false" />
<set-query-parameter name="productId" exists-action="override">
<value>@(context.Request.MatchedParameters["productId"])</value>
</set-query-parameter>
<set-backend-service id="apim-generated-policy" backend-id="product-function" />
</inbound>

问题是我还需要在函数本身设置routes

在我的function.json中,我添加了以下内容来匹配我的API,现在它工作了:

"route": "product-function/{productId?}"

最新更新