ASP.NET MVC5 异常后重定向到 ErrorPage

Par*_*tte 2 c# asp.net exception asp.net-mvc-5

我编写了一个自定义异常过滤器来记录我的应用程序异常,异常发生后我想将用户重定向到错误页面下面是我的代码

我的代码工作得很好,它捕获了异常,但在记录后它并没有将我扔到错误页面,你能帮忙吗

我的 CustomException Filer 类

public class CustomExceptionFilterAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        try
        {
            string requestBody = "", Action = "", Controller = "";

            try 
            {
                requestBody = filterContext.HttpContext.Request.Form.ToString();
                        Action = filterContext.RouteData.Values["action"].ToString();
                        Controller = filterContext.RouteData.Values["controller"].ToString();
            }
            catch (Exception)
            {
            }

            StringBuilder sbHeader = new StringBuilder();
            sbHeader.AppendLine(filterContext.RequestContext.HttpContext.Request.Headers.ToString());
            StaticMethods.LogException(SessionHelper.LoginCode.ToString(), Action, Controller, filterContext.Exception.Message, filterContext.RequestContext.HttpContext.Request.RawUrl.ToString(), requestBody, sbHeader.ToString());

            // This is which i am more concern about
            filterContext.RouteData.Values.Add("Error", filterContext.Exception.Message);
            filterContext.Result = new RedirectToRouteResult(
                                       new RouteValueDictionary(new { controller = "Error", action = "Error" }));
        }
        catch(Exception ex)
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的错误控制器

public class ErrorController : Controller
    {
        // GET: Error
        public ActionResult Error()
        {
            ViewBag["ErrorMessage"] = RouteData.Values["Error"];
            return View();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的Error.cshtml

<div class="alert alert-danger">
    <environment names="Development">
        <strong>Error!</strong> Some Error Occured.
    </environment>
    <environment names="Staging,Production">
        <strong>Error!</strong> @ViewBag.ErrorMessage
    </environment>
</div>
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗我只想在记录异常后重定向到错误页面它仍然向我显示抛出错误的黄页

谢谢

Abd*_*yed 5

不明白为什么要使用ErrorAttribute。您可以轻松地使用 Global.asax 文件来处理应用程序级别的错误。

 protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            //Log your exception here
            Response.Clear();
            string action= "Error";
            // clear error on server
            Server.ClearError();

            Response.Redirect(String.Format("~/Error/{0}?message={1}", action, exception.Message));

        }
Run Code Online (Sandbox Code Playgroud)