0

在 MVC 应用程序中,我们如何显示标准 MVC 错误页面以及使用 Microsoft 异常处理应用程序块处理异常?

我已经更新了我的 web.config 文件

这会在出现异常时重定向到故障视图。但是异常处理应用程序块(下面的代码)不再处理异常。ExceptionPolicy.HandleException(ex, "AllExceptions", out errorToThrow);

如何显示错误视图以及处理异常?我不想使用 ELMAH。

4

2 回答 2

1

更改您的 web.config 文件以显示自定义错误,这样您就可以显示错误页面。

然后为处理异常添加这个函数给你 global.asax:

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    //handle the exception
}
于 2012-10-10T06:35:19.100 回答
1

使用 Log4net 和 HttpHandler 进行日志记录和错误处理。请参阅以下示例代码。ExceptionHandler并用相同的属性装饰控制器。( [ExceptionHandler] public class BillsController : Controller { }

public class ExceptionHandler : HandleErrorAttribute
{
    private static readonly ILog log = LogManager.GetLogger(typeof(ExceptionHandler));

    public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
    {
        log.Debug("******** ExceptionHandler.OnException() *****************");

        base.OnException(filterContext);
        Exception ex = filterContext.Exception;
        if (filterContext.Exception is BusinessException)
        {
            log.Debug("<<<<Inside On BusinessException....>>>>" + DateTime.Now);
            //log.Debug("*** Error Getting From **** " + LOGIN_DETAILS.LoginUserName +
            //          "(UserId = " + LOGIN_DETAILS.LoginUserID + ")" + "Time =" + DateTime.Now);


            BusinessException _BusinessException =
                (BusinessException)filterContext.Exception;

            StringBuilder errormsg = new StringBuilder();
            foreach(MessageInfo msg in _BusinessException.ErrorMessageList)
            {
                errormsg.AppendLine(msg.LocalizedMessage);
            }

            log.Error("<<<<--------BusinessException-------->>>> : Exception Details -----"+ errormsg.ToString());
            //filterContext.ExceptionHandled = true;

            filterContext.ExceptionHandled = true;
            RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
            string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Account", action = "Logout", message = filterContext.Exception.Message })).VirtualPath;
            filterContext.HttpContext.Response.Redirect(url, true);
        }
        else
        {
             log.Error("Exception Details ---- " +
               "MESSAGE: " + ex.Message +
               "\nSOURCE: " + ex.Source +
               "\\Controller: " + filterContext.Controller.ToString() +
               "\nTARGETSITE: " + ex.TargetSite +
               "\nSTACKTRACE: " + ex.StackTrace,
               ex);

            filterContext.ExceptionHandled = true;
            RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
            string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Exception", action = "Default" })).VirtualPath;
            //string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "exception", action = "Default", message = filterContext.Exception.Message })).VirtualPath;
            filterContext.HttpContext.Response.Redirect(url, true);
        }

    }
}
于 2012-10-10T06:19:04.740 回答