【问题标题】:ASP.NET ActionResult gives different responses for localhost and Azure web service hostedASP.NET ActionResult 为 localhost 和托管的 Azure Web 服务提供不同的响应
【发布时间】:2017-09-13 22:44:52
【问题描述】:

我有以下代码

public ActionResult PerformMagic(string a, string b, int c)
{
    try
    {
        // Some code which always gives an error and go to catch block
    }
    catch (Exception ex)
    {
        // ex.Message = "An error occured"

        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return this.Content(System.Web.Helpers.Json.Encode(new { error = ex.Message }), "application/json");
    }
}

所以调用返回以下结果,

{
    config : {method: "GET", transformRequest: Array(1), transformResponse: Array(1), jsonpCallbackParam: "callback", paramSerializer: ƒ, …}
    data : 
        error : "An error occured"
    __proto__ : Object
    headers : ƒ (name)
    status : 400
    statusText : ""
    __proto__ : Object
}

所以,我在 JSON 中获取了 data,查找 error 并将值(即 An error occured)显示为警报。

在 localhost 中运行时效果很好,但是当将其部署到 Azure App 服务并运行时,响应如下

{
    config : {method: "GET", transformRequest: Array(1), transformResponse: Array(1), jsonpCallbackParam: "callback", paramSerializer: ƒ, …}
    data : "Bad Request"
    headers : ƒ (name)
    status : 400
    statusText : "Bad Request"
    __proto__ : Object
}

也就是说,我在data 中找不到error。谁能解释一下为什么会这样?

【问题讨论】:

  • 我的第一个猜测是您在本地启用了调试,并且正在 Azure 中部署不返回异常详细信息的发布版本?这是一件的事情,因为异常可能是信息泄漏。
  • @RickvandenBosch 即使在调试配置中部署应用程序时也有相同的行为

标签: c# asp.net json azure httpresponse


【解决方案1】:

事实证明,造成这种情况的原因在于httpErrors element。与本地计算机上的行为相比,我可以想象这个元素在 Azure 上具有不同的默认行为。

长话短说:您可以通过在 web.config 中的 system.WebServer 元素下添加它来解决它:

<httpErrors existingResponse="PassThrough" />

可能的值为 Auto (0)、Replace (1) 和 PassThrough (2):

我不完全确定此更改的安全影响。但是,它确实使您能够将(异常)详细信息返回给可能不属于那里的用户。

答案基于这篇帖子:ASP.NET+Azure 400 Bad Request doesn't return JSON data

【讨论】:

  • 工作 - 真的不知道。感谢您提供信息。
【解决方案2】:

确保两台机器(localhost 和 azure)都运行相同的 .NET Framework。否则,请检查处理序列化的 NuGet 包中是否存在任何奇怪的缓存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-12
    • 2020-02-18
    • 2020-04-27
    • 2021-07-06
    • 2017-02-04
    • 2013-09-24
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多