首页 > 解决方案 > Azure API 管理 - 如何访问产品信息

问题描述

在 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 属性。谢谢你。

标签: azureazure-api-management

解决方案


您可以使用 获取产品名称@(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,您将获得如下快照。

在此处输入图像描述


推荐阅读