BaseModuleRewriter.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Web;
  3. namespace URLRewriter
  4. {
  5. /// <summary>
  6. /// The base class for module rewriting. This class is abstract, and therefore must be derived from.
  7. /// </summary>
  8. /// <remarks>Provides the essential base functionality for a rewriter using the HttpModule approach.</remarks>
  9. public abstract class BaseModuleRewriter : IHttpModule
  10. {
  11. /// <summary>
  12. /// Executes when the module is initialized.
  13. /// </summary>
  14. /// <param name="app">A reference to the HttpApplication object processing this request.</param>
  15. /// <remarks>Wires up the HttpApplication's AuthorizeRequest event to the
  16. /// <see cref="BaseModuleRewriter_AuthorizeRequest"/> event handler.</remarks>
  17. public virtual void Init(HttpApplication app)
  18. {
  19. // WARNING! This does not work with Windows authentication!
  20. // If you are using Windows authentication, change to app.BeginRequest
  21. app.AuthorizeRequest += new EventHandler(this.BaseModuleRewriter_AuthorizeRequest);
  22. }
  23. public virtual void Dispose() {}
  24. /// <summary>
  25. /// Called when the module's AuthorizeRequest event fires.
  26. /// </summary>
  27. /// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the
  28. /// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks>
  29. protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
  30. {
  31. HttpApplication app = (HttpApplication) sender;
  32. Rewrite(app.Request.Path, app);
  33. }
  34. /// <summary>
  35. /// The <b>Rewrite</b> method must be overriden. It is where the logic for rewriting an incoming
  36. /// URL is performed.
  37. /// </summary>
  38. /// <param name="requestedRawUrl">The requested RawUrl. (Includes full path and querystring.)</param>
  39. /// <param name="app">The HttpApplication instance.</param>
  40. protected abstract void Rewrite(string requestedPath, HttpApplication app);
  41. }
  42. }