查询字符串参数
我见过很多读取查询字符串参数的示例。
例如,采用以下 URI:
/api/Profile?id={accountId}
可以使用以下语法在策略 xml 中读取 accountId
:
context.Request.Url.Query["accountId"]
路由参数
但是如果我的 URI 结构如下:
/api/profiles/{accountId}
当参数不在查询字符串中时,如何读取策略 xml 中的 accountId
?
最佳答案
使用上面的示例,我可以读取 accountId
通过以下语法:
context.Request.MatchedParameters["accountId"]
例如,我可以将其分配给一个变量,以便在 APIM 策略文件中的其他位置使用:
<set-variable name="account-id" value="@(context.Request.MatchedParameters["accountId"])" />
关于它的文档并不多,但幸运的是至少有 some mention of the Context variable在 learn.microsoft.com 上。
关于azure - 如何访问 Azure API 管理中的路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60548557/