Bokambo
Bokambo

Reputation: 4480

Application_Error does not handle exception

I am working on MVC Application.

I have FilterConfig class :

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

I am using it in Global.asax.cs

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

If i use this approach my Application_Error does not get fired when any exception occurs in controller.

 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");
        }

Now when i do customErrors mode="Off" it goes to Application_Error Event but HandleErrorInfo comes as 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")

Upvotes: 1

Views: 1440

Answers (1)

Rajshekar Reddy
Rajshekar Reddy

Reputation: 19007

This is because your web config settings <customErrors mode="On" /> are overriding the default behavior. You will need to disable this setting. if you disable this, you can handle the error in the Application_Error event then redirect the user or display a message from this event. This web config setting is handling the errors and only unhandled errors will bubble up to the Application_Error event.

Upvotes: 4

Related Questions