Asp. HttpModule of custom HTTP processing and application in net

Source: Internet
Author: User

HttpHandler implements a function similar to the ISAPI extention, which handles request information and sends a response (Response). The implementation of the HttpHandler function is achieved by implementing the IHttpHandler interface. The HttpModule implements a function similar to the ISAPI filter.

The realization of HttpModule

HttpModules implements a function similar to ISAPI filter, which usually takes the following steps in development:

1. Write a class that implements the IHttpModule interface

2. Implement the Init method, and register the required methods

3. How to Implement Registration

4. Implement the Dispose method, if you need to do some cleanup work for the class manually, you can add the implementation of the Dispose method, but this is not required, and you can usually add no code to the Dispose method.

5. In the Web. config file, register the class you wrote

The following is an example of a httpmodules that, in this example, simply registers the HttpApplication beginrequest and endrequest events and prints the relevant information through the implementation of these events.

[C-sharp]View Plaincopy
  1. Example 1:
  2. Using System;
  3. Using System.Web;
  4. Namespace MyModule
  5. {
  6. public class Mymodule:ihttpmodule
  7. {
  8. public void Init (HttpApplication application)
  9. {
  10. Application. BeginRequest + = (new
  11. EventHandler (This.  Application_BeginRequest));
  12. Application. EndRequest + = (new
  13. EventHandler (This.  Application_EndRequest));
  14. }
  15. private void Application_BeginRequest (Object source, EventArgs e)
  16. {
  17. HttpApplication application = (HttpApplication) source;
  18. HttpResponse Response=application.context.response;
  19. Response.Write ("
  20. }
  21. private void Application_EndRequest (Object source, EventArgs e)
  22. {
  23. HttpApplication application = (HttpApplication) source;
  24. HttpResponse Response=application.context.response;
  25. Response.Write ("
  26. }
  27. public void Dispose ()
  28. {
  29. }
  30. }
  31. }

The beginning of the program refers to the following namespaces:

Using system;using system.web;



Because classes such as HttpApplication, HttpContext, HttpResponse, and so on are defined in System.Web, the System.Web namespace must be referenced.

The MyModule class implements the IHttpModule interface. In the Init method, the methods for implementing BeginRequest and EndRequest events are indicated. In both of these methods, you simply print some information separately.

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

[C-sharp]View Plaincopy
    1. <configuration>
    2. <system.web>
    3. <add name="MyModule" type="MyModule, MyModule"/>
    4. </system.web>
    5. </configuration>


Now take a look at the effect. Write an ASPX page test.aspx, which reads as follows:

[C-sharp]View Plaincopy
    1. <%
    2. Response.Write ("
    3. %>


After running the interface:

In-depth study of HttpModule



HttpModule affects the HTTP processing pipeline by handling a series of events on the HttpApplication object, which is registered in the HttpModule init method, including:

Beginrequestauthenticaterequestauthorizerequestresolverequestcacheacquirerequeststateprerequesthandlerexecutepostrequesth Andlerexecutereleaserequeststateupdaterequestcacheendrequest



Some of these events correspond to the events in Global.asax, and the corresponding relationships are as follows:

HttpModule Global.asax
BeginRequest Application_BeginRequest
AuthenticateRequest Application_AuthenticateRequest
EndRequest Application_EndRequest



In Example 1, the beginrequest and endrequest events are handled, and the other events are handled basically similarly.

In correspondence with HttpHandler, some of these events occur before HttpHandler and some occur after the HttpHandler has been processed. It is important to understand the order in which events occur, because the server-side objects behave differently over different time periods. One example is the use of the session. Not all events can be processed in a session, but only in a limited number of events. The detailed procedure can refer to the following HTTP request processing life cycle diagram.

Using HttpModule to implement a permission system

When we develop the application system, the permission control of the application system is a very important part. In ASP, it is troublesome to control the permissions, because we must add the permission control code in each ASP page that needs control permission, thus controlling the customer's access to the page. This brings the problem, in addition to writing a large number of duplicate code, because the rights control part and the business processing part of the module tightly coupled, the rights control module modification, often brings a lot of modification work, and even caused a lot of bugs.

Therefore, we now need to decouple the privilege control and the business processing module so that two parts can be independently developed and modified without affecting each other, or minimizing the impact. In the JSP program, this can be achieved by introducing a front-end controller to implement the permission filtering (for the front-end controller mode, you can see the "Java EE Core mode book"). In ASP. NET, we can use HttpModule to achieve the same effect. Here's a look at the implementation process.

First, we will build a privilege processing system, can detect a user to a module function has access (concrete structure, I think, the reader should be exposed to this part of the programming, so no longer repeat), wherein the exposure to the client calls the definition of the right check class is as follows:

[C-sharp]View Plaincopy
    1. Public class Rightchecker
    2. {
    3. public static bool Hasright (User user,module Module)
    4. {
    5. //Perform permission checks,
    6. }
    7. }



Then we use HttpModule to write a filter:

[C-sharp]View Plaincopy
  1. Using System;
  2. Using System.Web;
  3. Namespace MyModule
  4. {
  5. public class Mymodule:ihttpmodule
  6. {
  7. public void Init (HttpApplication application)
  8. {
  9. Application. AcquireRequestState + = (new
  10. EventHandler (This.  Application_acquirerequeststate));
  11. }
  12. private void Application_acquirerequeststate (Object source,
  13. EventArgs e)
  14. {
  15. HttpApplication application = (HttpApplication) source;
  16. User user=application.context.sesseion["user"] //Get user
  17. string Url=application.context.request.path;
  18. Get a page for a customer visit
  19. Module module= //To get the modules that are located by URL
  20. If (! Rightchecker.hasright (User,module))
  21. Application.Context.Server.Transfer ("errorpage.aspx");
  22. If you do not have permissions, boot to the error-handling page
  23. }
  24. public void Dispose ()
  25. {
  26. }
  27. }
  28. }

Using this class as described earlier, after registering in Web. config, our application system has the function of rights management. How much better than the original way?

Conclusion

In. NET, Microsoft has greatly simplified the programming of the server extension which has the higher difficulty, it is very convenient for us to develop, it is worth our thorough research to this technology.

Asp. HttpModule of custom HTTP processing and application in net

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.