/// <summary> /// 处理进行Ajax请求时产生的异常 /// </summary> /// <param name="filterContext">异常上下文对象</param> protected override void HandleAjaxException(ExceptionContext filterContext) { if (filterContext.Exception is MessageException) { LogUtil.Warn(filterContext.Exception.ToString()); ResponseUtil.ResponseTextHtml("{\"status\":0,\"data\":\"" + filterContext.Exception.Message + "\"}"); } else if (filterContext.Exception is AuthorizationException) { ResponseUtil.ResponseTextHtml("{\"status\":3,\"data\":\"/User/Login\"}"); } else if (filterContext.Exception is BinderException) { LogUtil.Info(filterContext.Exception.ToString()); BinderException be = (BinderException)filterContext.Exception; if (be.Code == "0") { ResponseUtil.ResponseTextHtml("<script>parent.P.Set.TempCallBack('{\"status\":0,\"data\":\"" + filterContext.Exception.Message + "\"}');</script>"); } } else { LogUtil.Error(filterContext.Exception.ToString()); ResponseUtil.ResponseTextHtml("{\"status\":4,\"data\":\"" + filterContext.Exception.Message + "\"}"); } filterContext.Result = null; filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.StatusCode = 200; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; }
/// <summary> /// 捕获发生在应用程序中的错误 /// </summary> /// <param name="sender">sender</param> /// <param name="e">e</param> protected void Application_Error(object sender, EventArgs e) { #if DEBUG Exception ex = Server.GetLastError(); string message = string.Empty; if (ex != null) { if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404) { LogUtil.Info(ex.ToString()); } else { LogUtil.Error("Application_Error:" + ex.ToString()); } message = ex.ToString(); } Server.ClearError(); ResponseUtil.ResponseTextHtml("{\"status\":4,\"data\":\"" + message + "\"}"); HttpContext.Current.Response.StatusCode = 500; HttpContext.Current.Response.TrySkipIisCustomErrors = true; #else Exception exception = Server.GetLastError(); Response.Clear(); if (exception is HttpException && ((HttpException)exception).GetHttpCode() == 404) { Redirect404(); LogUtil.Info(exception.ToString()); } else { Redirect500(); LogUtil.Error(exception.ToString()); } //清除Exception,避免继续传递给上一级处理 Server.ClearError(); Response.TrySkipIisCustomErrors = true; #endif }
/// <summary> /// 处理非Ajax请求时产生的异常 /// </summary> /// <param name="filterContext">异常上下文对象</param> protected override void HandleException(ExceptionContext filterContext) { // 下面是临时演示,以后需按异常类型完善 if (filterContext.Exception is MessageException) { LogUtil.Warn(filterContext.Exception.ToString()); //Response.Write("<div style=\"padding-top:100px; margin:0 auto; text-align:center;\">"); //Response.Write("抱歉,发生了错误,参考消息:<span style='color:red;'>" + filterContext.Exception.Message + "</span>"); //Response.Write("<br /><br /><a href=\"javascript:history.back();\">单击此处返回</a>"); //Response.Write("</div>"); } else if (filterContext.Exception is AuthorizationException) { // Response.Redirect("/User/Login", true); Response.Redirect("/Apply/ApplyPrompt", true); } else if (filterContext.Exception is BinderException) { LogUtil.Info(filterContext.Exception.ToString()); BinderException be = (BinderException)filterContext.Exception; if (be.Code == "0") { ResponseUtil.ResponseTextHtml("<script>parent.P.Set.TempCallBack('{\"status\":0,\"data\":\"" + filterContext.Exception.Message + "\"}');</script>"); } } else { //此处日志由父类记录,避免重复记录 //LogUtil.Error(filterContext.Exception.ToString()); // 注要:如果想通过url传递任何参数,一定要防止注入攻击 base.HandleException(filterContext); } ViewBag.Message = filterContext.Exception.Message; ViewBag.StackTrace = filterContext.Exception.StackTrace; filterContext.Result = View("Error"); filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.StatusCode = 500; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; }
protected virtual void HandleAjaxException(ExceptionContext filterContext) { if (filterContext.Exception is MessageException) { LogUtil.Info("测试是否进入这里来"); LogUtil.Warn(filterContext.Exception.ToString()); ResponseUtil.ResponseTextHtml("{\"status\":2,\"data\":\"" + filterContext.Exception.Message + "\"}"); } else if (filterContext.Exception is AuthorizationException) { ResponseUtil.ResponseTextHtml("{\"status\":3,\"data\":\"" + filterContext.Exception.Message + "\"}"); } else { LogUtil.Error(filterContext.Exception.ToString()); ResponseUtil.ResponseTextHtml("{\"status\":4,\"data\":\"" + filterContext.Exception.Message + "\"}"); } filterContext.Result = null; filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.StatusCode = 500; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; }