Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomErrorHandlingModule : IHttpModule
- {
- #region Implementation of IHttpModule
- public void Init(HttpApplication context)
- {
- context.Error += (sender, e) =>
- OnError(new HttpContextWrapper(((HttpApplication)sender).Context));
- }
- public void Dispose()
- {}
- public void OnError(HttpContextBase context)
- {
- // Determine error resource to display, based on HttpStatus code, etc.
- // For brevity, i'll hardcode it for this SO question.
- const string errorPage = @"/Error/NotFound";
- // Now somehow execute the correct controller for that route.
- // Return the html response.
- }
- }
- public void OnError(HttpContextBase context)
- {
- context.ClearError();
- context.Response.StatusCode = 404;
- var rd = new RouteData();
- rd.Values["controller"] = "error";
- rd.Values["action"] = "notfound";
- IController controller = new ErrorController();
- var rc = new RequestContext(context, rd);
- controller.Execute(rc);
- }
- System.Web.HttpContext.Current.Response.Redirect("/Error/NotFound");
- // MVC 3 running on IIS 7+
- if (HttpRuntime.UsingIntegratedPipeline)
- {
- context.Server.TransferRequest(url, true);
- }
- else
- {
- // Pre MVC 3
- context.RewritePath(url, false);
- IHttpHandler httpHandler = new MvcHttpHandler();
- httpHandler.ProcessRequest(httpContext);
- }
- HttpApplication app = (HttpApplication) context.Application;
- app.CompleteRequest();;
Add Comment
Please, Sign In to add comment