【问题标题】:APIM Policy - Unable to cast objectAPIM 策略 - 无法投射对象
【发布时间】:2021-07-22 06:35:37
【问题描述】:

我在使用 APIM URL 重写策略时遇到问题,我需要使用变量的后缀值。我尝试了各种方法,但总是以错误结束“无法将'Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.PipelineResponse'类型的对象转换为'System.String” em>

这是我的入境政策:

 <inbound>
    <!-- Send Requst to Logic Apps to retrieve the corresponding suffix -->
    <send-request mode="new" response-variable-name="Response_Suffix" timeout="20" ignore-error="true">
        <set-url>{{LogicAppsURL}}</set-url>
        <set-method>POST</set-method>
        <set-header name="Content-Type" exists-action="append">
            <value>application/json</value>
        </set-header>
        <set-body>@{
                
                return new JObject(
                    new JProperty("ModelName", (context.Request.Body.As<JObject>(preserveContent: true).SelectToken("ModelName")))).ToString();

                }</set-body>
    </send-request>
    <set-variable name="URL_Suffix" value="@((IResponse)context.Variables["Response_Suffix"])" />

    <!-- Set Backend URL -->
    <set-backend-service base-url="{{BaseURL}}" />
    <base />

    <!-- Rewrite URL -->
    <rewrite-uri template="@((string)context.Variables["URL_Suffix"])" />
</inbound>

【问题讨论】:

    标签: json azure casting url-rewriting azure-api-management


    【解决方案1】:

    Response_Suffix 变量通过send-request 策略设置为IResponse 实例。然后,您的 set-variable 策略也将 URL_Suffix 变量设置为 IResponse。然后在 rewrite-url 策略中将URL_Suffix 转换为string。

    不确定 URL 后缀到底是什么意思,但您的 set-variable 策略可能是错误的,感觉您应该从响应中提取某些属性,而不是整个响应。

    【讨论】:

      【解决方案2】:

      我修改了逻辑应用程序的输出,并将 UR_Suffix 作为 json 正文中的标记(字符串)进行了转换,这显然解决了问题。

      JSON 输出:

      {"suffix":"<value>"}
      

      修改后的政策:

       <set-variable name="URL_Suffix" value="@((((IResponse)context.Variables["Suffix_Body"]).Body.As<JObject>().SelectToken("suffix")).ToString())" />
      

      【讨论】:

        猜你喜欢
        • 2018-12-18
        • 1970-01-01
        • 1970-01-01
        • 2013-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        相关资源
        最近更新 更多