【问题标题】:Azure API management - how to access product informationAzure API 管理 - 如何访问产品信息
【发布时间】:2020-08-19 14:48:58
【问题描述】:

在 APIM 中,我试图在 API 级别策略访问产品信息,因为我必须根据产品名称执行一些逻辑。我正在使用下面的代码

<policies>
    <inbound>
        <set-variable name="ProductName" value="@{
            return  context.Product.Name;
        }" />

但是,当尝试从邮递员发布请求时,我可以在跟踪中看到以下异常。

{
source: "set-variable",
timestamp: "2020-08-19T14:42:24.4936554Z",
elapsed: "00:00:00.0358409",
data:- {
messages:- [
-{
message: "Expression evaluation failed.",
expression: " return context.Product.Name; ",
details: "Object reference not set to an instance of an object."
},
"Expression evaluation failed. Object reference not set to an instance of an object.",
"Object reference not set to an instance of an object."
]
}
}

为什么它是 null ?是这种情况,我无法访问入站范围内的属性。需要指导。或者,有没有其他方法可以访问 Product.Name 属性。 谢谢。

【问题讨论】:

  • 如果对您有帮助,请采纳。点击复选框变为绿色。

标签: azure azure-api-management


【解决方案1】:

您可以使用@(context.Product.Name) 获取产品名称。

<inbound>
    <base />
    <set-variable name="aaa" value="@(context.Product.Name)" />
    <set-body template="liquid">
    {
        "success": true,
        "var1": {{context.Variables["aaa"]}}
    }
    </set-body>
</inbound>

在测试中,将产品名称设置为Starter,您将得到如下快照。

【讨论】:

  • 嗨乔伊蔡,感谢您的回复。对我来说,对象引用异常仅出现在第一个作业中。即 ,因为有问题共享异常,或者,我在这里错过了什么?
  • 只需使用@(context.Product.Name) 而不是@{ return context.Product.Name; }
  • 看看你在打什么电话。如果使用 API/服务订阅,context.Product 将为空。您可以在订阅列表中的 Azure 门户中查看订阅范围
猜你喜欢
  • 1970-01-01
  • 2020-03-16
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 2019-08-08
  • 1970-01-01
相关资源
最近更新 更多