Module A, which subscribes toEndRequest
Module B, which subscribes toBeginRequest and EndRequest
Module C, which subscribes toAuthenticateRequest
The order of execution will be:
Module B, BeginRequest
Module C, AuthenticateRequest
Module A, EndRequest
Module B, EndRequest
for each request , only ONEhandler will be mapped to do some logic, depending on the configuration in web.config.
<system.webServer> <modules> <addname="urlRewrittingModule"type="WebCode.asp.net.HttpModule.UrlRewriterModule"/> </modules> </system.webServer>
<system.webServer> <handlers> <addverb="*" name="urlRewritingHandler" path="*.aspx"type="WebCode.asp.net.HttpHandler.UrlRewriterHandler"/> </handlers> </system.webServer>
Whenever want to handle some request but no need the UI page
Authentication,Application log , Auditing , URLRewriting
public class UrlRewriterModule : IHttpModule { public voidDispose() { //clean-upcode here. } public voidInit(HttpApplication context) { context.AuthenticateRequest += ContextOnAuthenticateRequest; context.BeginRequest += ContextOnBeginRequest; context.AuthorizeRequest += context_AuthorizeRequest; context.PreRequestHandlerExecute += ContextOnPreRequestHandlerExecute; context.PostRequestHandlerExecute += ContextOnPostRequestHandlerExecute; context.EndRequest += ContextOnEndRequest; } #region BEFOREhandler private void ContextOnPreRequestHandlerExecute(objectsender, EventArgs eventArgs) { } private voidcontext_AuthorizeRequest(object sender, EventArgs e) { } private voidContextOnBeginRequest(object sender, EventArgs eventArgs) { //test URLrewritten varcontext = ((HttpApplication)sender).Context; if(context.Request.RawUrl.Contains("ExploreRedirect")) { context.RewritePath("ExploreRedirect.aspx","","SN="+ Guid.NewGuid()); } } private voidContextOnAuthenticateRequest(object sender, EventArgs eventArgs) { } #endregion #region AFTERhandler private voidContextOnEndRequest(object sender, EventArgs eventArgs) { } private voidContextOnPostRequestHandlerExecute(object sender, EventArgs eventArgs) { } #endregion }
public classUrlRewriterHandler : IHttpAsyncHandler { public voidProcessRequest(HttpContext context) { //dosomething } privateAction<HttpContext> _process; public boolIsReusable { get; private set; } publicIAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, objectextraData) { _process =ProcessRequest; return_process.BeginInvoke(context, cb, extraData); } public voidEndProcessRequest(IAsyncResult result) { _process.EndInvoke(result); } }