HttpModule configuration and dynamic configuration in Web. config

Source: Internet
Author: User

Study Notes

Asp. NET when processing HTTP request, using pipeline (pipeline) method, the request is processed by each HttpModule, and then arrives httphandler,httphandler after processing, is still processed by each HttpModule in pipeline, and the HTML is finally sent to the client browser.
Several very important objects are involved in the life cycle: httphandler,httpmodule,ihttphandlerfactory

Off Topic

HttpModule and Httphander

In the process of processing, the page handler undergoes Httpmodule,httphandler processing:

The former httpmodule is used for the processing of some events before and after the page processing, while the latter HttpHandler for real page processing

As previously mentioned, HttpModule will process the page before and after the page is processed, so it will not affect the actual page request

The difference between HttpModule and IHttpHandler
1. Sequencing. First IHttpModule, then IHttpHandler. Note: module depends on which event you are responding to, some of which are running before handler, some are running after handler

2. On the processing of the request:
IHttpModule is a size-all type that calls to it regardless of what file the client requests, such as Aspx,rar,html's request.
IHttpHandler is a picky eater, and only the file types that are registered by ASP (for example, Aspx,asmx, and so on) will be called.
3.IHttpHandler generates the content of the response according to your request, ihttpmodule the request, such as validation, modification, filtering, and so on, and can also handle the response

Configure in Web. config (not available in Classic mode OH)

1. Definition inherits the IHttpModule class

2. Registering events

3. In the Web. config configuration

Create Class

public class Myhttpmoudle:ihttpmodule
{

public void Init (HttpApplication context)
{
Context. BeginRequest + = Context_beginrequest;
}

void Context_beginrequest (object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app! = null)
{
App. Context.Response.Write ("Hello");

}
}

public void Dispose ()
{

}
}

Configuration

Under the system.web node

<add name= "Test" type= "Webs review. Util.myhttpmoudle"/>

This inserts the custom HttpModule class Myhttpmodule into the pipeline of the current web HttpModule. The main function of HttpModule is to intercept the various events of application, and to complete their own processing in these events. In fact, if you develop some projects, directly in the Global.asax processing is enough. If you are developing a framework or some aspect of a component, you need to add processing to the event to develop a custom
HttpModule, you can avoid using the framework or components and manually add code to the Global.asax. Now think of the purpose of developing custom HttpModule, there are global identity/authorization verification, custom site access/operation log records, in the management/debugging for the purpose of monitoring the site tracking and so on.

Dynamic Registration HttpModule

If we do not want to be troublesome in the configuration file configuration, we can dynamically configure. HttpApplication will initialize all configuration files in the initialization of httpmodules, of course, this is the first, but fortunately, the second dynamic loading in the absence of a read configuration file, which is not compiled before you can also complete the addition of HttpModule function , because, reading a configuration file is a process in HttpApplication, so it should be possible to think that if you want to add HttpModule it's definitely on the HttpApplication, actually. It's in the Hostingenvironment class.

An Microsoft.Web.Infrastructure.dll file is provided when ASP. MVC3 is released, which provides the ability to dynamically register HttpModule

The static class of the Assembly Dynamicmoduleutility has a registermodule that can be added to the HttpModule class we define.

But by looking at the method, the process is as follows

1. Create a class that implements the IHttpModule

2. Assembly marked as Preapplicationstartmethodattribute attribute

3. Create a class that contains a static method that registers a custom class

Because a method of hostingenvironment traverses all the assemblies under the application

Determines if the assembly is marked as a Preapplicationstartmethodattribute attribute, the method specified in this attribute is executed (static method)

The specific implementation process is as follows

Defining classes

public class Myhttpmoudle:ihttpmodule
{

public void Init (HttpApplication context)
{
Context. BeginRequest + = Context_beginrequest;
}

void Context_beginrequest (object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app! = null)
{
App. Context.Response.Write ("Custom HttpModule");

}
}

public void Dispose ()
{

}
}

Intermediate Class (contains a static registration method)

public class Registermodules
{

private static bool Hasload;
public static void Loadpre ()
{
if (!hasload)
{
Hasload = true;

namespace Microsoft.Web.Infrastructure.DynamicModuleHelper;

Dynamicmoduleutility.registermodule (typeof (Myhttpmoudle));
}
}
}

Finally, add the following features as required

[Assembly:preapplicationstartmethod (typeof (Registermodules), "Loadpre")]

Run results

Summarize:

1. There are two ways to configure HttpModule, one to add directly to Web. config, the second dynamic configuration, but to introduce an assembly

2, the demand is not very big words we can directly use the Global.asax configuration

3. Dynamic load Registration Custom HttpModule can only be used once

HttpModule configuration and dynamic configuration in Web. config

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.