我在部署ASP时遇到了一个非常奇怪的行为。Net网站到Azure。部署到Azure时,自定义筛选器的行为不同。问题是在自定义异常过滤器中生成的JsonResult没有在Azure中序列化JSON对象或其他内容。
这是中的浏览器控制台中打印的自定义筛选器的结果
发展
:
对象{data:对象,状态:400,配置:对象,状态文本:“错误请求”}
我可以看到“数据”是一个对象,具有以下属性:
数据:对象
details:“堆栈详细信息…”
消息:“需要网站!”
在Azure中,同样的事情是:
对象{数据:“错误请求”,状态:400,配置:对象,状态文本:“错误的请求”}
config:对象
data:“错误请求”
我可以看到数据只是字符串!
我的自定义过滤器如下:
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);
}
}
有人能指出发生了什么事吗?