asp.net framework Depth Adventure (2)

Source: Internet
Author: User
Tags config file system http request httpcontext implement
asp.net asp.net framework Depth adventure (2)

Author:uestc95
ArticleType: Original
E-mail:uestc95@263.net
The. NET Framework version:1.0.3705 Official edition
Vs. NET (C #) version:7.0.9466 Official edition


Just finished dinner, just to exercise the finger on the keyboard.
Then go on to write this "diary":

Chapter Two--how does HttpModule work?

We said last time that a HTTP request from the client was intercepted and passed through layers (why buck?) OH) reached the HttpModule this "request listener".
HttpModule is similar to the placement in aspnet_wp. EXE process of a bug, a little bit of common sense people will naturally imagine what a bug is used to do, and our HttpModule
It's a perfect choice for a bug, but it needs to be clear that HttpModule is definitely not a simple listener, it can do more things like it can add some content to the interception request.
Wait a minute.
It is also necessary to understand that when an HTTP request arrives at HttpModule, the entire ASP.net framework system does not have any real processing of the request, but we can
Append some of the information we need before this HTTP request is delivered to the Real Request Processing Center (HttpHandler) in this HTTP request first, or against the HTTP that we intercepted
Request information to do some extra work, or in some cases simply terminate the HTTP Request that satisfies some conditions, so that it acts as a filter, not just a bug.
By looking at MSDN (don't trust the QuickStarts Web document that came with the. NET SDK, there are many places in the official version where this document is not updated, many things are not valid in the official version),
You will find that the system HttpModule implement a interface called IHttpModule, it is natural to think that as long as our own class can implement the IHttpModule interface, can not completely replace the system
Are you HttpModule? Absolutely right.
Before we start our own HttpModule class, let me tell you what the HttpModule is like in the system, ASP. NET system, the default HttpModule are as follows:
System.Web.Caching.OutputCacheModule
System.Web.SessionState.SessionStateModule
System.Web.Security.WindowsAuthenticationModule
System.Web.Security.FormsAuthenticationModule
System.Web.Security.PassportAuthenticationModule
System.Web.Security.UrlAuthorizationModule
System.Web.Security.FileAuthorizationModule

Well, let's start our own HttpModule construction process.
1 Open vs.net a new "Class Library" item and name it myhttpmodule.
2) referencing System.Web.dll files

In the code area, typing:

Using System;
Using System.Web;

Namespace Myhttpmoduletest
{
<summary>
Description: The class used to implement custom HttpModule
Author: uestc95
Contact: uestc95@263.net
</summary>
public class Myhttpmodule:ihttpmodule
{
<summary>
Description: Constructor method
Author: uestc95
Contact: uestc95@263.net
</summary>
Public Myhttpmodule ()
{

}

<summary>
Description: The Init method for implementing the IHttpModule interface
Author: uestc95
Contact: uestc95@263.net
</summary>
<param name= "Application" >httpapplication type of parameter </param>
public void Init (HttpApplication application)
{
Application. BeginRequest +=new EventHandler (this. Application_BeginRequest);
Application. EndRequest +=new EventHandler (this. Application_EndRequest);
}

<summary>
Description: A private method that you define to do something
Author: uestc95
Contact: uestc95@263.net
</summary>
<param name= "obj" > Object parameters passed in </param>
<param name= "E" > Event parameters </param>
private void Application_BeginRequest (Object Obj,eventargs e)
{
HttpApplication application= (HttpApplication) obj;
HttpContext context=application. context;
HttpResponse Response=context. Response;
HttpRequest Request=context. Request;

Response. Write ("I come from Application_BeginRequest,:)");

}

<summary>
Description: A private method that you define to do something
Author: uestc95
Contact: uestc95@263.net
</summary>
<param name= "obj" > Object parameters passed in </param>
<param name= "E" > Event parameters </param>
private void Application_EndRequest (Object Obj,eventargs e)
{
HttpApplication application= (HttpApplication) obj;
HttpContext context=application. context;
HttpResponse Response=context. Response;
HttpRequest Request=context. Request;

Response. Write ("I come from Application_EndRequest,:)");

}

<summary>
Description: Implementation of the IHttpModule interface of the Dispose method
Author: uestc95
Contact: uestc95@263.net
</summary>
public void Dispose () {}
}
}

3 after compiling in vs.net, you will get MyHttpModule.dll this file.
4 The next step is how to get the Aspnet_wp.exe process to give the HTTP request to the HttpModule we wrote ourselves? The method is to configure the Web.config file.
Add the following words to the Web.config file:
<add name= "test" type= "Myhttpmoduletest.myhttpmodule,myhttpmodule"/>
Note that you want to be case-sensitive because Web.config is case sensitive as an XML file. "Type=myhttpmoduletest.myhttpmodule,myhttpmodule" tells us
The HTTP request request will be handed over to the Myhttpmoduletest.myhttpmodule class located in the MyHttpModule.dll file. And this DLL file system will automatically
to the \ Bin subdirectory or the system global assembly buffer (GAC) search. We can put the DLL file we just got in the bin subdirectory, and for the latter, you can use the. NET SDK Official edition
With the Config tool we do, we don't say it in detail.


Well, our HttpModule to intercept HTTP request requests is complete and assembled, and you can try to build a new WebForm in your Web project and run a look at it. :)
Finally, let's assume an occasion to use this httpmodule. A site to provide free asp.net virtual space for everyone, but a site managers do not want to provide a free lunch, he wants in every
When the page is browsed, it automatically pops up its own company's ads (as it is now www.X63.com), I can't always monitor all the pages of all users and want to add them manually on every page.
A piece of JS code, the workload is unimaginable, is also unrealistic. Well, as soon as our httpmodule is hooked up, it's all going to be easy, as long as we're in every
When Http request is captured by us, it is better to add some JS to him!

We mentioned above that two event beginrequest and endrequest are used in the init () method, which are the first and final events of all the events that can be handled in Init (), in their
There are other events in the middle that we can use to check MSDN.

In addition, before I close editplus, I need to knock down the following words:
You can use response,request,server,application normally in HttpModule, but you can't manipulate any of the session-related code!
Why, then? Think about it for yourself, next time to see where the reason is, in addition to a question, you can find the system default to the number of HttpModule where configured? Look for it.

Next time we look at the HttpHandler part and how to match the HttpModule.

You later.

(To be continued, welcome to explore: uestc95@263.net)



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.