void Application_Error(object sender, EventArgs e)
        {
            ErrorDAL  DAL        = new ErrorDAL();
            Exception ex         = Server.GetLastError().GetBaseException();
            var       requestUrl = Request.Url.ToString();

            DAL.InsertErrorLog(requestUrl, ex.Message, ex.StackTrace);

            if (ex is HttpException)
            {
                if (((HttpException)ex).GetHttpCode() == 404)
                {
                    Response.Redirect("/Error/Error404", true);
                }
            }
            else
            {
                string excMessageSafe = Regex.Replace(ex.ToString(),
                                                      "(\\<+[a-z!/\\?])|(&\\#)", new MatchEvaluator((m) =>
                {
                    return(m.Value.Replace("<", string.Empty).Replace("&#", string.Empty));
                }), RegexOptions.IgnoreCase);
                //string errorDetailsQueryString = System.Web.Configuration.WebConfigurationManager.AppSettings["show_detailed_errors"] == "true" ? "?ErrorTitle=" + HttpUtility.UrlPathEncode(exc.GetType().Name) + "ErrorMessage" + HttpUtility.UrlPathEncode(excMessageSafe.Substring(0, 1200)) : "";
                Response.Redirect("/Error/Error500", true);
            }
        }
예제 #2
0
        protected override void OnException(ExceptionContext filterContext)
        {
            ErrorDAL DAL = new ErrorDAL();

            filterContext.ExceptionHandled = true;

            var ex = filterContext.Exception; // TPDO : Log Exception

            var requestUrl = Request.Url.ToString();

            DAL.InsertErrorLog(requestUrl, ex.Message, ex.StackTrace);

            #region Use full Code


            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                var urlHelper = new UrlHelper(filterContext.RequestContext);
                filterContext.HttpContext.Response.StatusCode = 403;
                filterContext.Result = new JsonResult
                {
                    Data = new
                    {
                        Error = ex.Message
                    },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            else
            {
                if (ex is HttpException)
                {
                    if (((HttpException)ex).GetHttpCode() == 404)
                    {
                        //Redirect to 404 page:
                        filterContext.Result = RedirectToAction("Error404", "Error");
                    }
                }
                else
                {
                    string excMessageSafe = Regex.Replace(ex.ToString(),
                                                          "(\\<+[a-z!/\\?])|(&\\#)", new MatchEvaluator((m) =>
                    {
                        return(m.Value.Replace("<", string.Empty).Replace("&#", string.Empty));
                    }), RegexOptions.IgnoreCase);
                    // Redirect on error:
                    //var ErrorObject = System.Web.Configuration.WebConfigurationManager.AppSettings["show_detailed_errors"] == "true" ? new { ErrorTitle = ex.GetType().Name, ErrorMessage = excMessageSafe.Substring(0, 1400) } : null;

                    filterContext.Result = RedirectToAction("Error500", "Error");
                }
            }

            #endregion
        }