验证参数Azure APIM策略未按URL路径参数的预期工作



我有azure HTTP触发器功能,具有路由定义的

public async Task<IActionResult> FetchUser(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/users/{profileid}")]
HttpRequest req,
string profileid)

APIM网址:https://sampleapidemo-apim-poc.azure-api.net/v1/users/PROFILEIDTOBEENTER

我在APIM中导入了azure函数应用程序,并试图创建一个入站策略来检查是否提供了url路径参数中的配置文件id。

我尝试了以下两种方法。

<validate-parameters specified-parameter-action="prevent" unspecified-parameter-action="prevent" errors-variable-name="requestUrlValidation">
<path specified-parameter-action="prevent">
<parameter name="profileid" action="prevent" />
</path>
</validate-parameters>
<set-variable name="profileParameter" value="@(context.Request.Url.Path.Contains("profileid"))" />
<choose>
<when condition="@((bool)context.Variables["profileParameter"] == false)">
<return-response>
<set-status code="400" reason="Bad Request" />
<set-body>
<value>Profile Id is missing in url path.</value></set-body>
</return-response>
</when>
<otherwise />
</choose>

实际输出:当点击URLhttps://sampleapidemo-apim-poc.azure-api.net/v1/users/https://sampleapidemo-apim-poc.azure-api.net/v1/users

{
"statusCode": 404,
"message": "Resource not found"
}

预期输出:当点击URLhttps://sampleapidemo-apim-poc.azure-api.net/v1/users/https://sampleapidemo-apim-poc.azure-api.net/v1/users

{
"statusCode": 400,
"message": "Bad Request. Profile ID is missing in URL path"
}

由于您已将{profileid}定义为APIM URL中的模板参数,因此不能省略它。如果{profileid}为空,则APIM无法识别URL。因此,错误404。如果您使用查询参数,它可能会起作用:

不要做:https://sampleapidemo-apim-poc.azure-api.net/v1/users/{profileid}

您可以执行以下操作:https://sampleapidemo-apim-poc.azure-api.net/v1/users

查询参数:

profileid

结果:https://sampleapidemo-apim-poc.azure-api.net/v1/users?profileid={profileid}

你可以在Azure函数中获取参数值,如下所示:

string profileid= req.Query["profileid"];

最新更新