private void HandleCustomErrors(Exception exception) { var httpException = exception as HttpException; if (httpException != null) { var status = httpException.GetHttpCode(); if (status == 404 || status == 403) { var routeData = new RouteData(); RouteHelper.InitErrorRoute(status, routeData); // Clear the error on server. Server.ClearError(); // Avoid IIS7 getting in the middle Response.StatusCode = status; Response.TrySkipIisCustomErrors = true; // idially we should get controller throught servicelocator IController errorController = new ErrorController(); errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); } } }
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { IController controller; try { controller = base.GetControllerInstance(requestContext, controllerType); } catch (HttpException exc) { var httpCode = exc.GetHttpCode(); if (httpCode == 404) { Log.Error("Cannot instantiate mvc controller.", exc); controller = base.GetControllerInstance(requestContext, typeof(ErrorController)); requestContext.RouteData.Values.Clear(); RouteHelper.InitErrorRoute(httpCode, requestContext.RouteData); } else { throw; } } return(controller); }