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

问题描述 投票:2回答:5

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

我的代码完全正常,它捕获异常,但在记录后它不会让我错误页面,你能帮忙吗?

我的自定义异常过滤器类

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

这是我的ErrorController

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
5个回答
3
投票

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

        }

0
投票

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

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

0
投票

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

return View();

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

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

0
投票

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

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

0
投票

在ASP.NET MVC 5中,您可以使用:

if(/*Some error*/){
    return View("Error");
}
© www.soinside.com 2019 - 2024. All rights reserved.