Guest User

Untitled

a guest
Jul 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. public class CustomErrorHandlingModule : IHttpModule
  2. {
  3. #region Implementation of IHttpModule
  4.  
  5. public void Init(HttpApplication context)
  6. {
  7. context.Error += (sender, e) =>
  8. OnError(new HttpContextWrapper(((HttpApplication)sender).Context));
  9. }
  10.  
  11. public void Dispose()
  12. {}
  13.  
  14. public void OnError(HttpContextBase context)
  15. {
  16. // Determine error resource to display, based on HttpStatus code, etc.
  17. // For brevity, i'll hardcode it for this SO question.
  18. const string errorPage = @"/Error/NotFound";
  19.  
  20. // Now somehow execute the correct controller for that route.
  21. // Return the html response.
  22. }
  23. }
  24.  
  25. public void OnError(HttpContextBase context)
  26. {
  27. context.ClearError();
  28. context.Response.StatusCode = 404;
  29.  
  30. var rd = new RouteData();
  31. rd.Values["controller"] = "error";
  32. rd.Values["action"] = "notfound";
  33. IController controller = new ErrorController();
  34. var rc = new RequestContext(context, rd);
  35. controller.Execute(rc);
  36. }
  37.  
  38. System.Web.HttpContext.Current.Response.Redirect("/Error/NotFound");
  39.  
  40. // MVC 3 running on IIS 7+
  41. if (HttpRuntime.UsingIntegratedPipeline)
  42. {
  43. context.Server.TransferRequest(url, true);
  44. }
  45. else
  46. {
  47. // Pre MVC 3
  48. context.RewritePath(url, false);
  49.  
  50. IHttpHandler httpHandler = new MvcHttpHandler();
  51. httpHandler.ProcessRequest(httpContext);
  52. }
  53.  
  54. HttpApplication app = (HttpApplication) context.Application;
  55. app.CompleteRequest();;
Add Comment
Please, Sign In to add comment