Application_Error不处理异常

时间:2016-02-17 11:59:04

标签: asp.net-mvc global-asax

我正在研究MVC应用程序。

我有FilterConfig类:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }
}

我在Global.asax.cs

中使用它
protected void Application_Start()
{            
      AreaRegistration.RegisterAllAreas();
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
      RouteConfig.RegisterRoutes(RouteTable.Routes);
      BundleConfig.RegisterBundles(BundleTable.Bundles);
}

如果我使用这种方法,当控制器中发生任何异常时,我的Application_Error不会被触发。

 protected void Application_Error()
        {
            var exception = Server.GetLastError();
            AppLogging.WriteError(exception);
            var httpException = exception as HttpException;
            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();
            routeData.Values["controller"] = "Error";
            routeData.Values["action"] = "Error";
            routeData.Values["exception"] = exception;
            Response.StatusCode = 500;
            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch (Response.StatusCode)
                {
                    case 403:
                        routeData.Values["action"] = "UnauthorizedAccess";
                        break;
                    case 503:
                        routeData.Values["action"] = "SiteUnderMaintenance";
                        break;
                    case 404:
                        routeData.Values["action"] = "PageNotFound";
                        break;
                }
            }
            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;
            IController errorsController = new ErrorController();
            HttpContextWrapper wrapper = new HttpContextWrapper(Context);
            var rc = new RequestContext(wrapper, routeData);
            errorsController.Execute(rc);
        }

 public ActionResult Error()
        {
            return View("Error");
        }

现在,当我执行customErrors mode =“Off”时,它转到Application_Error事件,但HandleErrorInfo为null。

Error.cshtml

@model HandleErrorInfo

@{
    Layout = "~/Views/Shared/_LayoutAnonymous.cshtml";
    ViewBag.Title = "Error";
}
<div class="error_wrapper">
    <div class="col-sm-12 col-xs-12">
        <div class=" error_col">   
            <div style="display: none;">
                @if (Model != null)
                {
                    <h3>@Model.Exception.GetType().Name</h3>
                    <pre>
                @Model.Exception.ToString()
               </pre>
                    <p>
                        thrown in @Model.ControllerName @Model.ActionName
                    </p>
                }
            </div>             
                <h1 class="error_h1">503 </h1>
                <h2 class="error_h2">Looks like we're having some server issues. </h2>
                <h3 class="error_h3">
                    Go back to the previous page and try again.<br>
                    If you think something is broken, report a problem.
                </h3>
                <div class="col-sm-12 col-xs-12 padding_none">
                    <button class="btn btn-primary btn_box error_btn" id="btnReport">Report A Problem</button>
                    <button class="btn btn-primary btn_box error_btn pull-left" onclick="location.href='@Url.Action("Requests", "Pricing", new RouteValueDictionary { { "area", "" } })'"> Go To Homepage</button>
                </div>
            </div>

    </div>
</div>

@Scripts.Render("~/bundles/Error")

1 个答案:

答案 0 :(得分:3)

这是因为您的网络配置设置<customErrors mode="On" />会覆盖默认行为。您需要禁用此设置。如果禁用此功能,则可以处理Application_Error事件中的错误,然后重定向用户或显示此事件的消息。此Web配置设置正在处理错误,只有未处理的错误才会冒泡到Application_Error事件。