【发布时间】: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