Пример #1
0
        /// <summary>
        /// The application_ error. Catches Unhandled Exceptions.
        /// And shows a custom error page.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = this.Server.GetLastError();
            this.Server.ClearError();

            var routeData = new RouteData();
            routeData.Values.Add("controller", "Error");
            routeData.Values.Add("action", "Error");
            routeData.Values.Add("exception", exception);

            routeData.Values.Add(
                "statusCode",
                exception.GetType() == typeof(HttpException) ? ((HttpException)exception).GetHttpCode() : 500);

            this.Response.TrySkipIisCustomErrors = true;
            IController controller = new ErrorController();
            controller.Execute(new RequestContext(new HttpContextWrapper(this.Context), routeData));
            this.Response.End();
            Log.Error(exception);
        }
Пример #2
0
        protected void Application_Error(Object sender, EventArgs e)
        {
            var       exception = Server.GetLastError();
            RouteData route     = new RouteData();

            route.Values["Controller"] = "Error";
            if (exception is HttpException httpException)
            {
                switch (httpException.GetHttpCode())
                {
                case 404:
                    route.Values["Action"] = "NotFound";
                    Response.StatusCode    = 404;
                    break;

                case 403:
                    route.Values["Action"] = "Forbidden";
                    Response.StatusCode    = 403;
                    break;

                default:
                    route.Values["Action"] = "InternalServerError";
                    Response.StatusCode    = 500;
                    break;
                }
            }
            else
            {
                route.Values["Action"] = "InternalServerError";
                Response.StatusCode    = 500;
            }
            Server.ClearError();
            Response.TrySkipIisCustomErrors = true;
            IController errorController = new Controllers.ErrorController();

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