[Asp.net Basics] httpmodule, httphandler, and handlerfactory (not complete to be continued, and asynchronous processing)

Source: Internet
Author: User

ASP. NET Request Processing Process:

Httprequest --> inetinfo.exe --> response --> aspnet_wp.exe --> httpruntime --> httpapplication factory --> httpapplication --> httpmodule --> httphandler factory --> httphandler. processrequest ()

Through the flowchart, we can see that in the Asp.net request pipeline, the httpmodule processes the previous and subsequent work in the pipeline, and the actual processing in the middle is performed by aspx or ashx. as mentioned above, httpmodule processes pages before and after page processing, so it does not affect real page requests. It is usually used to add some information (such as the copyright statement) to the header or tail of each page, or in multiple languages and permissions.

Create a new myhttpmodule and myhttphandler,CodeAs follows:

Register the following events in the initialization method of myhttpmodule

 
Public VoidInit (system. Web. httpapplication Application)
 
{
Application. beginrequest + =NewEventhandler (application_beginrequest );
 
Application. endrequest + =NewEventhandler (application_endrequest );
 
Application. prerequesthandlerexecute + =NewEventhandler (application_prerequesthandlerexecute );
 
Application. postrequesthandlerexecute + =NewEventhandler (application_postrequesthandlerexecute );
 
Application. releaserequeststate + =NewEventhandler (application_releaserequeststate );
 
Application. acquirerequeststate + =NewEventhandler (application_acquirerequeststate );
Application. authenticaterequest + =NewEventhandler (application_authenticaterequest );
 
Application. authorizerequest + =NewEventhandler (application_authorizerequest );
 
Application. resolverequestcache + =NewEventhandler (application_resolverequestcache );
 
Application. presendrequestheaders + =NewEventhandler (application_presendrequestheaders );
 
Application. presendrequestcontent + =NewEventhandler (application_presendrequestcontent );
 
}

The substitution of each event is similar to the following:

 
VoidApplication_beginrequest (ObjectSender, eventargs E)
 
{
 
Httpapplication application = (httpapplication) sender;
Application. Context. response. Write ("Application_beginrequest <br/>");
 
}

Myhttphandler

Public VoidProcessrequest (system. Web. httpcontext context)
 
{
 
Context. response. contenttype ="Text/html";
 
Context. response. Write ("Process all requests");
 
}

Register them in webconfig

 
<Httpmodules>
<Add Name= "Myhttpmodule" Type= "Testmodule. myhttpmodule, testmodule"/>
 
</Httpmodules>
 
<Httphandlers>
 
<Add Verb= "*" Path= "*" Type= "Testmodule. myhttphandler, testmodule"/>
</Httphandlers>
 
Httpmodule can register multiple. The pipeline processing sequence is determined by the registration order.
 
Name httpmodule, name: class name, type:ProgramSet. Class Name, assembly
 
Httphandler, verb: access method (*, get, post), path: Suffix of processing (*, *. aspx, *. ashx)
 
Http: // localhost: 10153/webform1.aspx
Output
Application_beginrequest
Global_application_beginrequest
Application_authenticaterequest
Global_application_authenticaterequest
Application_authorizerequest
Application_resolverequestcache
Application_acquirerequeststate
Application_prerequesthandlerexecute
Process all requests application_postrequesthandlerexecute
Application_releaserequeststate
Application_endrequest
Application_presendrequestheaders
We can see that aspx requests are processed by handler constructed by ourselves, while httpmodule adds some things at the beginning and end of the processing pipeline. At this time, we will look at the flowchart to see it clearly.
 

Ihttphandlerfactory Interface

the interface is used to create and manage HTTP handlers that process requests. Therefore, you can create a class that implements the ihttphandlerfactory interface and use the class as an HTTP handler.

using this method to create a handler allows you to better control the processing of HTTP requests. In this way, you can map URLs to HTTP handler factories that create different handlers based on a set of conditions. For example, by using an HTTP processing program factory, you can create a limited number of HTTP processing program objects to access expensive or limited resources such as database connections. Then, you can reuse these handler objects in future requests.

interface method

name description
gethandler return implementation ihttphandler Interface.
releasehandler allows the factory to reuse existing processing program instances.
ClassHandlerfactory: system. Web. ihttphandlerfactory
 
{
 
PublicSystem. Web. ihttphandler gethandler (system. Web. httpcontext context,StringRequesttype,StringURL,StringPathtranslated)
 
{
 
Ihttphandler handlertoreturn =Null;
 
If("Get"= Context. Request. requesttype. tolower ())
 
Handlertoreturn =NewMyhttphandler ();
 
Else If("Post"= Context. Request. requesttype. tolower ())
Handlertoreturn =NewMyhttphandler2 ();
 
ReturnHandlertoreturn;
 
}
 
 
 
Public VoidReleasehandler (system. Web. ihttphandler handler)
 
{
 
}
 
}

Modify the configuration

 
<Httphandlers>
<Add Verb= "*" Path= "*. T1, *. T2" Type= "Testmodule. handlerfactory, testmodule"/>
 
</Httphandlers>

Now, httphandlerfactory is used to process URLs with the suffix. T1 and. T2. In gethandler, it is determined which one will process the request.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.