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 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, perform corresponding processing for different page requests (determined based on the suffix)Program. For example,. aspx,. asmx,. skin, and so on all have corresponding type definition handlers.
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>
/// Description of handlertest Abstract
/// </Summary>
///
Namespace Hanlderandmodule
... {
Public Class Imagehandler: ihttphandler
... {
//
Public Void Processrequest (httpcontext context)
... {
Context. response. Write (
" <H1> This is an httphandler test. " );
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 < Httphandlers >
< 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 command to find that no matter what you do on the page when you access default. aspx and xxx/a.doc, the page will only appear:This is an httphandler test.
Your browser:

Type: IE6
Version: 6.0

access to other resources is displayed normally.

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.