Some basic knowledge of Dan after pain-custom httpmodule and httphandler

Source: Internet
Author: User

Another day of Dan's pain !!

What to do, customize something. Let's take httpmodule and httphandler to get started!

I. Custom httpmodule

The custom httpmodule needs to implement the ihttpmodule interface. The two methods in the interface need to be implemented in the inheritance class, the init and dispose methods; The init method has a parameter httpapplication, we can get some attributes or customize some processing for this parameter.Program, What can be dug in httpapplication, this can be seen by yourself to implement the following; because I am doing the demo, so the init method registered the beginrequest processing program,CodeAs follows:

 
Public void Init (httpapplication application) {application. beginrequest + = new eventhandler (application_beginrequest );}
 
 
 
Void application_beginrequest (Object sender, eventargs e) {httpcontext context = (sender as httpapplication). Context; context. response. Write ("custom configuration successful ");}

After writing the program, you need to configure it in Web. config. The configuration is very simple.

 
<Httpmodules> <Add name = "myhttpmodule" type = "myhttpmodule. myhttpmodule, webapplication1"/>  

Note that the parameter type = "myhttpmodule. myhttpmodule, webapplication1" is: namespace + processing class, assembly; sometimes the namespace and assembly are different.

Ii. Custom httphandler

Same as above: the ihttphandler interface must be inherited to implement the methods in the interface: processrequest and attributes: isreusable (whether the flag is cached)

Code:

 
Public class myhandler: ihttphandler {public void processrequest (httpcontext context) {context. response. write ("<br/> Hello World");} public bool isreusable {get {return false ;}}}

You also need to configure in Web. config

<Httphandlers> <add verb = "*" type = "webapplication1.myhandler, webapplication1" Path = "*. aspx"/>  

Specific parameters: verb-submit method: Post, get .... * All; Type: namespace + handler class, assembly name; Path: file to be processed (you can customize the type, such :. myfoot, but it needs to be configured in IIS. The basic aspx will not be used)

In fact, the Code shows that the init method parameter of myhttpmodule is httpapplication, while the parameter of myhandler method processrequest is httpcontext. It can be concluded that myhttpmodule occurred before myhandler, therefore, you must write the required functions in the correct processing program.

Okay, it's time to get off work at. Hope you can beat it !!

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.