Principles and Applications of httphandler, httpmodule, and ihttphandlerfactory in Asp.net (II)

Source: Internet
Author: User

In Asp.net, the principles and applications of httphandler, httpmodule, and ihttphandlerfactory (I) mentioned that httpmodule will be executed before and after page processing, while httphandler is the real page processing. Check C:/Windows/Microsoft. NET/framework/v2.0.50727/config/Web. config. You will find that there are many definitions about httpmodule and httphandler. For example, httpmodule defines the check and control of permissions, roles, sessions, and so on before and after page processing, while httphandler defines, execute corresponding processing programs for different page requests (determined based on the suffix. for example. aspx ,. asmx ,. skin and so on all have corresponding type definition handler.
Now that httphandler defines the processing program for specific page requests. If we customize httphandler, for the page we define, if the client has some requests, it will only be processed according to our defined handler. (That is, our custom processing replaces the original base processing ).
Its applications are also explicit: for example, resource permission control can be performed. If those pages require certain permissions to access (for example, in an IP segment), or the custom warning information will be output, you can also use httphandlercontrol (for example, the .doc file) If clientcommunicates with urldirectly in-site resources ).
The following is an example of msdn:
1. Create a processing class

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Globalization;
Using system. IO;

/** // <Summary>
/// Summary description of handlertest
/// </Summary>
///
Namespace hanlderandmodule
...{
Public class imagehandler: ihttphandler
...{
//
Public void processrequest (httpcontext context)
...{
Context. response. Write ("Context. response. Write ("<p> your browser: </P> ");
Context. response. Write ("type:" + context. Request. browser. Type + "<br> ");
Context. response. Write ("version:" + context. Request. browser. version );
}
// Override the isreusable property.
Public bool isreusable
...{
Get... {return true ;}
}
}
}

As you can see, the above custom method actually implements an ihttphandler method: processrequest, an attribute: isreusable. In fact, request processing is all processed in processrequest.
2. Create a DLL file in Z and add it to the bin directory.
3. Configure web. config <Add verb = "*" Path = "default2.aspx" type = "hanlderandmodule. imagehandler, hanlderandmodule"/>
<Add verb = "*" Path = "*. Doc" type = "hanlderandmodule. imagehandler, hanlderandmodule"/>
</Httphandlers>

It is defined here. If the client requests the default2.aspx page or directly requests the. DOC file, this custom processing program is called.
Description: verb indicates the client's request type. Path indicates the page for which the handler is called. type indicates the handler path, which is defined in the same way as httmodule.
4. create several Doc documents, create several pages, and run the discovery: when accessing the default. aspx, and xxx/a.doc, no matter what you have done on the page, the page will only show: This is an httphandler test.
Your browser:

Type: IE6
Version: 6.0

Access other resources is displayed normally.
To be continued ......

Related Article

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.