【问题标题】:ASP.NET MVC5 Redirect to ErrorPage after Exception异常后 ASP.NET MVC5 重定向到 ErrorPage
【发布时间】:2019-03-13 23:18:43
【问题描述】:

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

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

我的 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)
        {
        }
    }
}

这是我的错误控制器

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

这是我的 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>

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

谢谢

【问题讨论】:

标签: c# asp.net exception-handling asp.net-mvc-5


【解决方案1】:

不明白为什么要使用 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));

        }

【讨论】:

    【解决方案2】:

    你需要在 web.config 中添加你的错误页面

    <system.web>
      <customErrors mode="On" defaultRedirect="~/Error/Index">
        <error statusCode="500" redirect="~/Error/YourPage"/>             
      </customErrors>
    <system.web/>
    

    【讨论】:

    • 但我也希望在我的错误页面中出现错误,仅用于开发环境,
    【解决方案3】:

    如果要将其重定向到另一个页面,则需要指定目录。

    return View();
    

    这基本上意味着返回带有错误视图袋的当前页面。 你可以试试

    return redirect("~/Error"); //return redirect("url of the page")
    

    【讨论】:

    • 它没有重定向到错误控制器
    【解决方案4】:

    这是一个建议。你会尝试这样做吗?

    filterContext.Result = new RedirectToRouteResult(
                                           new RouteValueDictionary { { "Error", "Error" } });
    

    【讨论】:

      【解决方案5】:

      在 ASP.NET MVC 5 中,你可以使用这个:

      if(/*Some error*/){
          return View("Error");
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-13
        • 1970-01-01
        • 2020-01-19
        • 1970-01-01
        • 2016-03-21
        • 2011-08-19
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多