About httpmodule and httphandler

Source: Internet
Author: User
Tags dnn
I remember reading an article a long time ago, saying that I don't understand httpmodule, httphandler and httpcontext are very good ASP. NET programmers. From this point of view, before that, I was not a good ASP. NET programmer.

To understand the above several items, we must first understand how the server processes the request and returns the processing result to the client after an httprequest is sent to the server. In asp.net, when an httprequestarrives at the server, the hacker will first be intercepted by inetinfo.exe and then transferred to aspnet_isapi.dll for processing. The aspnet_isapi.dllwill forward the request to an httppipelinepipeline. The aspnet_wp.exe process will receive the request and send it to httpruntime for processing. For example

So how does the request work in this httpruntime? We can use the following process:

Httprequest ----> httpapplicationfactory ----> Generate an httpapplication instance ----> httpmodule ----> httphandlerfactory ----> httphandler ----> transfer the generated result back to the client. here we can see httpmodule and httphandler!

First, let's look at httpmodule. What is it?
The definition on msdn: http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/cpguide/html/cpconhttpmodules. asp
We can see that we can customize the programming httpmodule to process or filter the content in httprequest. The following link can turn to an msdn example: http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/cpguide/html/cpconcustomhttpmodules. asp. I will not talk about it here.

In this example, we can see that by registering the beginreqeust and endrequest events in httpapplication In the init method of the ihttpmodule interface, we can process different tasks in different stages. But in fact, httpapplication contains more events, see the msdn link: http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/cpref/html/frlrfsystemwebhttpapplicationclasstopic. asp

Let's look at httphanlder. Let's review the events in the httpapplication mentioned above:

[Execute the processing program.]

Postrequesthandlerexecute

When you enter this step, httpmodule begins to transfer the request to httphandler for processing, and the processed result is forwarded to httpmodule and sent back to the client. So how does httphandler work? In fact, similar to httpmodule, The ihttphanler interface is implemented, and then the handler is registered in Web. config for execution.

You can refer to msdn for how to execute and examples. I think it may be better to understand it. Link: http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/cpguide/html/cpconaspnetrequestprocessing. asp

At last, httpcontext exists after the instance of httpapplication is created, so you can easily access some information in httprequest. However, we recommend that you first understand the lifecycle of some objects before using them. Otherwise, exceptions may occur. For more information, see ASP. NET Framework deep adventure.

Practice: dnn uses a large number of custom httpmodule and httphandler to handle these things. So if you have the conditions, study dnn to see what these things can do for us.

References:
ASP. NET Framework deep adventure
Dotnetnuke friendly URLs
Msdn
Http://www.microsoft.com/china/msdn/library/architecture/architecture/architecturetopic/BuildSucApp/BSAAsecmodsecmod37.mspx? MFR = true

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.