Http Handler And Http Module

Http Module and Http Handler

. Relation

Http Handler And Http Module_第1张图片


.Page Event Pipe

Http Handler And Http Module_第2张图片


For Http Module:

  1. ·for each request, all http moduleswill be go through .
  2. ·if have multiple modules ,execute order ? for example :

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 Http Handler

for each request , only ONEhandler will be mapped to do some logic, depending on the configuration in web.config.

.configuration :

for http module :

<system.webServer>
<modules>
      <addname="urlRewrittingModule"type="WebCode.asp.net.HttpModule.UrlRewriterModule"/>
</modules>
</system.webServer>


for http handler:

<system.webServer>
<handlers>
      <addverb="*" name="urlRewritingHandler" path="*.aspx"type="WebCode.asp.net.HttpHandler.UrlRewriterHandler"/>
</handlers>
</system.webServer>


when use

Http Handler

Whenever want to handle some request but no need the UI page

Http Module

Authentication,Application log , Auditing , URLRewriting

code example :

http module(using http module to rewrite url) :

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
    }
 


http handler:

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);
        }
    }

你可能感兴趣的:(handler)