Example #1
0
        private void ShowCustomErrorPage(Exception exception)
        {
            var httpException = exception as HttpException ?? new HttpException(500, "Internal Server Error", exception);

            this.Response.Clear();
            var routeData = new RouteData();
            routeData.Values.Add("controller", "Error");
            routeData.Values.Add("fromAppErrorEvent", true);

            switch (httpException.GetHttpCode())
            {
                case 403:
                    routeData.Values.Add("action", "HttpError");
                    break;

                case 404:
                    routeData.Values.Add("action", "HttpError");
                    break;

                case 500:
                    routeData.Values.Add("action", "HttpError");
                    break;

                default:
                    routeData.Values.Add("action", "GeneralError");
                    routeData.Values.Add("httpStatusCode", httpException.GetHttpCode());
                    break;
            }

            this.Server.ClearError();

            IController controller = new ErrorController();
            controller.Execute(new RequestContext(new HttpContextWrapper(this.Context), routeData));
        }
        protected void Application_Error(object sender, EventArgs e)
        {
            var exception = Server.GetLastError();
            if (exception != null)
            {
                Response.Clear();
                HttpException httpException = exception as HttpException;
                RouteData routeData = new RouteData();
                routeData.Values.Add("controller", "Error");
                if (httpException == null)
                {
                    routeData.Values.Add("action", "Unknown");
                }
                else
                {
                    switch (httpException.GetHttpCode())
                    {
                        case 404:               // Page not found.
                            routeData.Values.Add("action", "NotFound");
                            break;

                        default:
                            routeData.Values.Add("action", "Unknown");
                            break;
                    }
                }


                // Pass exception details to the target error View.
                routeData.Values.Add("Error", exception);
                // Clear the error on server.
                Server.ClearError();
                // Avoid IIS7 getting in the middle
                Response.TrySkipIisCustomErrors = true;
                // Ensure content-type header is present
                Response.Headers.Add("Content-Type", "text/html");
                // Call target Controller and pass the routeData.
                IController errorController = new ErrorController();
                errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ErrorControllerTests" /> class.
 /// </summary>
 public ErrorControllerTests()
 {
     this.controller = new ErrorController();
 }