我正在使用 <send-request>
发出请求政策,我需要检查请求的状态代码。为此,我尝试访问 response-variable-name
的 StatusCode 属性。 。但是,当使用此代码时 -
context.Variables["LogFailedEvent"].StatusCode
我看到这个错误 -
Error in element 'set-body' on line 83, column 4: 'object' does not contain a definition for 'StatusCode' and no extension method 'StatusCode' accepting a first argument of type 'object' could be found
根据docs -
The
response-variable-name
attribute is used to give access the returned response. The name defined in this property can be used as a key into thecontext.Variables
dictionary to access theIResponse
object.
要么我误解了文档中的内容,要么文档是错误的。有人可以提出解决方案吗?
<小时/>政策相关部分
<choose>
<when condition="@(context.Response.StatusCode != 201 && context.Response.StatusCode != 202)">
<send-request mode="new" response-variable-name="LogFailedEvent" timeout="20" ignore-error="true">
<set-url>@(string.Format("{0}{1}?code={2}&statusCode={3}&statusReason={4}&message={5}", "{{BVT_LogEventsFailedFunction_Url}}", "{{BVT_LogEventsFailedFunction_Template}}", "{{BVT_LogEventsFailedFunction_Code}}", context.Response.StatusCode, context.Response.StatusReason, context.Response.Body.As<string>()))</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@(context.Request.Body.As<string>())</set-body>
</send-request>
<set-body>@(string.Format("{{\"message\":\"event captured by LogFailedEvents\",\"requestId\":\"{0}\",\"statusCode\":\"{1}\",}}", context.RequestId.ToString(), context.Variables["LogFailedEvent"].StatusCode))</set-body>
</when>
</choose>
最佳答案
您需要转换它:
((IResponse)context.Variables["LogFailedEvent"]).StatusCode
此外,由于您设置了ignore-error =“true”,您可能需要首先检查变量是否包含此键,因为在这种情况下,如果请求失败,则根本不会创建此变量。
关于azure - 访问状态代码或使用 <send-request> 发出的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45548508/