Analysis of ASP. net http Module

Source: Internet
Author: User

Brief Description

I have been working on a project in the past six months, the company's front-end MES system. next, we have been considering permission card control recently. so I re-studied the HTTP Module I saw before. I remember talking to a colleague a long time ago about the role of the HTTP module, which is still vivid. the topic discussed at that time was how to perform website statistical analysis. I said that if it is ASP. net, it is a good way to use the HTTP module. because the HTTP module can be reused. you only need to configure the web. config can be reused by multiple websites, which is suitable for the current situation of the company. then I made some comments and analyzed some asp. NET application lifecycle. the final conclusion is that the HTTP module can be implemented and is convenient. further expand to ASP. net website, that can be done, as long as you re-configure IIS. (Well, note that this discussion is limited to the IIS environment of Windows ). after a long time, I did not touch the idea of the website's statistical analysis. discover yourself ~~ ~ It's a fantasy ==| | well, let's summarize the HTTP module, summarize it in stages, and sort out your knowledge.

HTTP module Overview

The HTTP module is an assembly called every time a request is sent to an application. the HTTP module is called as part of the request pipeline. It can access lifecycle events throughout the request process. therefore, the HTTP module allows you to check incoming requests and perform operations based on the requests. they also enable you to check the outgoing response and modify it. therefore, the HTTP module has the following purposes:

Security

Statistics and logging

Custom header or footer

URL rewrite

 

In fact, ASP. NET uses modules to implement various application functions, including forms authentication, caching, session Status and client script tasks.

How the HTTP module works

The module must be registered to receive notifications from the request pipeline. the most common sending Method for registering an HTTP module is in the web of the application. config. IIS 7.0 has another registration method. after the module is registered, it can work in the application. the procedure is as follows:

1. When ASP. NET creates an instance that represents the httpapplication class of the application, it creates any registered module instance.

2. When creating a module instance, call the init method of the module and initialize it on your own.

3. subscribe to various application events in the init Method

4. When an application event is triggered, the corresponding method in the module is called.

5. When the module's event handler completes running, ASP will call the next process in the pipeline. This may be another module or an HTTP handler for the request.

HTTP module and Global. asax File

Global. asax file (also known as ASP.. NET application file) is an optional file that contains the response to ASP. code of application-level and session-level events caused by the net or HTTP module.

  HTTP Module Global File
Advantages The module can be encapsulated, so you can use it in many different applications after creating one time. By adding them to the Global Assembly Cache and registering them to the machineconfig file, they can be reused across applications. The global file allows you to process other registered events, such as session_start and session_end. In addition, the global file allows you to instantiate global objects that can be used throughout the application.
Usage conditions
  • You want to reuse this module in other applications.

  • Avoid placing complex code in the global. asax file.

  • This module applies to all requests in the MPs Queue (IIS 7.0 integration mode only ).

  • When you must subscribe to events that are unavailable to a module (such as session_start ),
    When creating code that depends on application events and does not need to reuse code across applications

    HTTP module custom events can be subscribed to in the Global File, such as session_start, which is actually subscribed to the start event of the session module.

    Simply put, this is the case. I defined an event in the module. What is the processing function of this event? When do I register this event?

    A: For example, I have customized a module class named sessionmodule. In this module, I have customized an event calledStart, UseSessionThe module name is registered in Web. config.

    In global. asax, declare a name:Session_startThis function is registered in this way.Module Registration Name Is session start eventLa

    The handler is the function named session_start.

    Procedure for creating a custom HTTP Module

    1. Create a class that implements the ihttpmodule interface.

    2. Compile a processing program for the init method. The initialization method should initialize the module and subscribe to any application events required.

    3. write code for subscribed events.

    4. If the module needs to be cleaned up, you can also choose to implement the dispose method.

    5. register the module in the web. config file.

     

    Continue to follow up ~~ A simple HTTP module for customizing headers and footers

    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.