Exemple #1
0
        public override void OnException(ExceptionContext filterContext)
        {
            // First, log any errors that come through here.
            Log.Error(filterContext.Exception);

            // Then, check if we should show pretty error pages, as opposed to the default ASP.NET style.
            if (!WebConfig.Get <bool>("Environment:IsLocalDev"))
            {
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
                filterContext.HttpContext.Response.StatusCode             = 500;
                filterContext.ExceptionHandled = true;
                filterContext.HttpContext.Response.Clear();

                using (var httpErrorController = new HttpErrorController())
                {
                    httpErrorController.ControllerContext = new ControllerContext(filterContext.HttpContext, new RouteData(), httpErrorController);
                    filterContext.Result = httpErrorController.Status500();
                }
            }

            base.OnException(filterContext);
        }
Exemple #2
0
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();

            var httpException = exception as HttpException;

            var routeData = new RouteData();

            routeData.Values.Add("controller", "HttpError");

            if (httpException != null)
            {
                switch (httpException.GetHttpCode())
                {
                case 404:
                    // Page not found.
                    routeData.Values.Add("action", "Status404");
                    break;

                case 500:
                    // Server error.
                    routeData.Values.Add("action", "Status500");
                    break;

                // Here you can handle Views to other error codes.
                default:
                    routeData.Values.Add("action", "Status500");
                    break;
                }
            }

            // Call target Controller and pass the routeData.
            IController errorController = new HttpErrorController();

            errorController.Execute(new RequestContext(
                                        new HttpContextWrapper(Context), routeData));
        }