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

解决方法

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

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...