首页 > 解决方案 > 在 Azure API 管理入站策略部分中获取路由参数

问题描述

我有一个带有模板参数的 URL。

https://test.azure-api.net/HelperFunction/{siteId}/lots/InventoryItem/{itemId}
https://test.azure-api.net/HelperFunction/122/lots/InventoryItem/12

我想阅读入站策略部分中的模板/路径参数。

我正在尝试如下。但它只会获取查询字符串参数。我想获取路径参数。

 <inbound>
        <set-body>@{
                JObject transBody = new JObject();
                transBody.Add("Arguments", 
                new JObject
                {
                    {"method", context.Request.Method},
                    {"parameters", context.Request.Url.QueryString},
                });

                //Add all json properties as arg
                transBody.Add("UriPath", context.Request.Url.Path);
                return transBody.ToString();
            }</set-body>
        <base />
 </inbound>

我的要求是将路由参数读取为

"siteId" : 122,
"itemId" : 12

任何帮助,将不胜感激。

标签: azureazure-api-management

解决方案


context.Request.MatchedParameters["siteId"]context.Request.MatchedParameters["itemId"]

其余的上下文变量可以在这里找到:https ://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions#ContextVariables


推荐阅读