Custom HTTP module in ASP. Net

Source: Internet
Author: User

HttpHandler implements functions similar to ISAPI Extention. It processes Request information and sends Response ). The HttpHandler function is implemented through the IHttpHandler interface. HttpModule implements functions similar to ISAPI Filter.

HttpModule implementation

HttpModules implements functions similar to ISAPI Filter. Generally, the following steps are required during development:

  1. Write a class to implement the IhttpModule Interface
  2. Implement the Init method and register the required Method
  3. Registration Method
  4. To implement the Dispose method, if you need to manually clear the class, you can add the implementation of the Dispose method, but this is not necessary. Generally, you can not add any code for the Dispose method.
  5. Register the class you have written in the Web. config file

The following is an example of HttpModules. In this example, only the BeginRequest and EndRequest events of HttpApplication are registered, and relevant information is printed out through the implementation of these events.

Example 1:
 

Using System;
Using System. Web;
Namespace MyModule {
Public class MyModule: IHttpModule {
Public void Init (HttpApplication application ){
Application. BeginRequest + = (new
EventHandler (this. Application_BeginRequest ));
Application. EndRequest + = (new
EventHandler (this. Application_EndRequest ));
}
Private void Application_BeginRequest (Object source, EventArgs e ){
HttpApplication Application = (HttpApplication) source;
HttpResponse Response = Application. Context. Response;
Response. Write ("}
Private void Application_EndRequest (Object source, EventArgs e)
{
HttpApplication application = (HttpApplication) source;
HttpResponse Response = Application. Context. Response;
Response. Write ("}
Public void Dispose (){}
}
}

The following namespace is referenced at the beginning of the program:

Using System;

Using System. Web;

Because classes such as HttpApplication, HttpContext, and HttpResponse are defined in System. Web, System. Web namespaces must be referenced.

The MyModule class implements the IhttpModule interface. The Init method specifies the method for implementing the BeginRequest and EndRequest events. In the two methods, some information is simply printed separately.

Next, register this class in the Web. config file and you can use this HttpModule. The registration method is as follows:

<Configuration>

<System. web>

<HttpModules>

<Add name = "MyModule" type = "MyModule, MyModule"/>

</HttpModules>

</System. web>

</Configuration>

Now let's take a look at the effect. Compile An Aspx page test. aspx with the following content:
<%

Response. Write ("

%>

After running:

HttpModule processes a series of events of the HttpApplication object to influence the HTTP processing pipeline. These events are registered in the Init method of HttpModule, including:
BeginRequest, AuthenticateRequest, AuthorizeRequest, ResolveRequestCache

AcquireRequestState, PreRequestHandlerExecute, PostRequestHandlerExecute

ReleaseRequestState, UpdateRequestCache, and EndRequest

Some of the events correspond to events in Global. asax. The corresponding relationship is as follows:

HttpModule Global. asax
BeginRequest Application_BeginRequest
AuthenticateRequest Application_AuthenticateReques
EndRequest Application_EndRequest

In example 1, The BeginRequest and EndRequest events are processed. The processing methods for other events are similar.

According to HttpHandler, some of these events occur before HttpHandler, and some occur after HttpHandler completes processing. It is very important to understand the sequence of events, because the objects on the server have different performance in different time periods. One example is the use of Session. Not all events can process sessions, but only a limited number of events. For detailed procedures, refer to the following HTTP Request processing lifecycle diagram.

Use HttpModule to implement the Permission System

When developing an application system, permission control of the application system is very important. In ASP, permission control is troublesome, because we must add permission control code to each ASP page that requires permission control to control access to the page. In addition to writing a large number of repeated code, the permission control module is closely coupled with the business processing module to modify the permission control module, it often brings about a lot of modifications and even a lot of bugs.

Therefore, we need to decouple the permission control and business processing modules so that the two parts can be developed and modified independently without affecting each other or minimizing the impact. In Jsp programs, this goal can be achieved by introducing a front-end controller to implement permission filtering (for details about the front-end controller mode, see the J2EE Core mode book). In ASP. Net, we can use HttpModule to achieve the same effect. The following describes the implementation process.

First, we will build a permission processing system that can detect whether a user has access to a module's functions (the specific structure, I think, readers should have access to this part of programming, so I will not go into details). The definitions of the permission verification class exposed to client calls are as follows:

Public class RightChecker {
Public static bool HasRight (User user, Module module) {// perform permission verification ,}}
Then, we use HttpModule to write a filter:
Using System;
Using System. Web;
Namespace MyModule {
Public class MyModule: IHttpModule {
Public void Init (HttpApplication application ){
Application. AcquireRequestState + = (new
EventHandler (this. Application_AcquireRequestState ));
}
Private void Application_AcquireRequestState (Object source, EventArgs e ){
HttpApplication Application = (HttpApplication) source;
User user = Application. Context. Sesseion ["User"] // obtain the User
String url = Application. Context. Request. Path; // obtain the page accessed by the customer
Module module = // obtain the Module based on the url
If (! RightChecker. HasRight (user, module ))
Application. Context. Server. Transfer ("ErrorPage. aspx ");
// If you do not have the permission, go to the error handling page
}
Public void Dispose ()
{
}
}
}

After registering this class in Web. Config according to the method described above, our application system will have the permission management function. How is it better than the original method?

Conclusion

In. in. Net, Microsoft has greatly simplified the programming of server extension that previously had relatively high difficulty, which has indeed brought great convenience to our development, it is worth our in-depth research on this technology.

Turn: http://www.uml.org.cn/net/200708145.asp

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.