部署 Azure 网站时的奇怪行为 - ASP.net MVC 和自定义错误筛选器
Strange behavior when deploying an Azure Website - ASP.net MVC and Custom Error Filter
我在将 ASP.Net 网站部署到 Azure 时遇到了一个非常奇怪的行为。自定义筛选器在部署到 Azure 时表现不同。问题是在自定义异常筛选器中生成的 JsonResult 没有序列化 Azure 中的 JSON 对象或其他对象。
这是 开发 中浏览器控制台打印的自定义过滤器的结果:
对象{数据:对象,状态:400,配置:对象,statusText:"Bad Request"}
我可以看到 "data" 是一个对象并具有以下属性:
数据:对象
详细信息:“堆栈详细信息...”
消息:"Website is required!"
在 Azure 中,同样的东西看起来是:
对象{数据:"Bad Request",状态:400,配置:对象,statusText:"Bad Request"}
配置:对象
数据:"Bad Request"
我看到数据只有字符串!
我的自定义过滤器如下:
public class CustomErrorFilter : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
//No further processing when the exception is handled
if (filterContext.ExceptionHandled)
{
return;
}
if (!(filterContext.Exception is BusinessException)) // Non business exceptions
{
//Logging the actual exception
ElmahLogger.LogError(filterContext.Exception);
System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);
//Changing exception type to a generic exception
filterContext.Exception = new SystemException("An error occurred and logged during processing of this application. We appologise for any in-convieneces.");
HandleAjaxException(filterContext, HttpStatusCode.InternalServerError);
filterContext.ExceptionHandled = true;
}
else
{
System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);
HandleAjaxException(filterContext, HttpStatusCode.BadRequest);
filterContext.ExceptionHandled = true;
}
}
private static void HandleAjaxException(ExceptionContext filterContext, HttpStatusCode httpStatusCode)
{
filterContext.HttpContext.Response.StatusCode = (int)httpStatusCode;
**// Seems like this is not working in Azure somehow????**
filterContext.Result = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
message = filterContext.Exception.Message,
details = filterContext.Exception.StackTrace
}
};
System.Diagnostics.Trace.TraceError("HandleAjaxException" + filterContext.Exception.Message);
}
}
谁能指出这是怎么回事?
好吧,我终于明白了。需要更改 web.config 文件,因为 IIS8 默认不显示详细错误。这修复了它:在 <system.webServer>
.
中添加 <httpErrors existingResponse="PassThrough"/>
行
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>
</configuration>
这将确保将上游错误呈现给浏览器。
我在将 ASP.Net 网站部署到 Azure 时遇到了一个非常奇怪的行为。自定义筛选器在部署到 Azure 时表现不同。问题是在自定义异常筛选器中生成的 JsonResult 没有序列化 Azure 中的 JSON 对象或其他对象。
这是 开发 中浏览器控制台打印的自定义过滤器的结果:
对象{数据:对象,状态:400,配置:对象,statusText:"Bad Request"} 我可以看到 "data" 是一个对象并具有以下属性: 数据:对象 详细信息:“堆栈详细信息...” 消息:"Website is required!"
在 Azure 中,同样的东西看起来是:
对象{数据:"Bad Request",状态:400,配置:对象,statusText:"Bad Request"} 配置:对象 数据:"Bad Request"
我看到数据只有字符串!
我的自定义过滤器如下:
public class CustomErrorFilter : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
//No further processing when the exception is handled
if (filterContext.ExceptionHandled)
{
return;
}
if (!(filterContext.Exception is BusinessException)) // Non business exceptions
{
//Logging the actual exception
ElmahLogger.LogError(filterContext.Exception);
System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);
//Changing exception type to a generic exception
filterContext.Exception = new SystemException("An error occurred and logged during processing of this application. We appologise for any in-convieneces.");
HandleAjaxException(filterContext, HttpStatusCode.InternalServerError);
filterContext.ExceptionHandled = true;
}
else
{
System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);
HandleAjaxException(filterContext, HttpStatusCode.BadRequest);
filterContext.ExceptionHandled = true;
}
}
private static void HandleAjaxException(ExceptionContext filterContext, HttpStatusCode httpStatusCode)
{
filterContext.HttpContext.Response.StatusCode = (int)httpStatusCode;
**// Seems like this is not working in Azure somehow????**
filterContext.Result = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
message = filterContext.Exception.Message,
details = filterContext.Exception.StackTrace
}
};
System.Diagnostics.Trace.TraceError("HandleAjaxException" + filterContext.Exception.Message);
}
}
谁能指出这是怎么回事?
好吧,我终于明白了。需要更改 web.config 文件,因为 IIS8 默认不显示详细错误。这修复了它:在 <system.webServer>
.
<httpErrors existingResponse="PassThrough"/>
行
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>
</configuration>
这将确保将上游错误呈现给浏览器。