How to: Create an ASP.NET HTTP Module (from MSDN) using Visual C # .NET (from MSDN)

xiaoxiao2021-03-06  74

The release number of this article has been CHS307996

This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.

For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.

For Microsoft Visual Basic .NET versions of this article, see

308000.

This task content

summary

Implementation module deployment module configuration system test module reference

SUMMARY This step-by-step guide demonstrates how to create a simple custom HTTP module using Visual C # .NET. This article demonstrates how to create, deploy, and configure this module, and how to mount events for the module in the global.asax file.

Back to top

Implementation module

Newly built a Visual Studio .NET C # class library project called MyModule. Set a reference to the System.Web.dll library. Add the following instructions to this class: use system.Web; rename the syncmodule.cs class, then change the class definition accordingly. Implement the IHTTPModule interface. Your class definition style is as follows: public class syncmodule: ihttpmodule decides which events are booked. The following summarizes the events that can be booked from the HTTPAPPLICATION object:

AcquireRequestState: Call this event to allow modules to get or create a status of the requested state (for example, a session). AuthenticateRequest: When the security module needs to verify this event when verifying the user's identity before processing the request. Author Zeequest: This event can be called by the security module when requesting to be authorized. This event is called after authentication. BeginRequest: Call this event to inform a new request for a module being started. DISPOSED: This event is called to notify the module application to terminate for some reason. Allow the module to perform internal cleaning. EndRequest: Call this event to inform the module new request is ending. Error: Calling this event notification module has an error in the request processing. PostRequestHandleRexecute: Call this event to notify the module handler has completed the process of request. PrerequestHandleRexecute: Call this event to notify the module to handle the procedure for processing the request is being called. PresendRequestContent: Call this event to inform the module, the content is being sent to the client. PresendRequestHeaders: Call this event to inform the module, the HTTP header is being sent to the client. ReleaseRequestState: Calling this event allows the module to release the state after the process has completed the request. ResolveRequestCache: This event is called after authentication. The cache module uses this event to determine if the request should be handled by its cache, or whether a handler should process the request. UpdateRequestCache: This event is called after getting a response from the handler. The cache module should update their cache based on this response. This example uses the BeginRequest event. Please implement the IHTTPMODULE interface in the following manner: public void init (httpApplication app)

{

App.BeginRequest = New EventHandler (OnBeginRequest);

}

Public void dispose () {} is an event creation appointed appointment: Public Delegate Void MyEventhandler (Object S, Eventargs E); Defines a private local variable record of a MyEventHandler type to this event: private myeventhandler _eventhandler = null; Creating an event will be attached to the method in the Global.asax file or the class inherited from the HTTPApplication object. Public evenet myeventhandler myevent {

Add {_eventhandler = value;

REMOVE {_EventHandler - = value;

} Create an OnBeginRequest method, it hits the BeginRequest event of HttpApplication: Public Void OnbeginRequest (Object S, Eventargs E)

{

HTTPApplication App = S as httpapplication;

App.Context.Response.write ("Hello from OnbeginRequest In Custom Module.
");

IF (_eventhandler! = NULL)

_eventhandler (this, null);

} Compiled the project.

Back to top

Deployment module

Create a directory named module under C: / INETPUB / WWWROOT. Create a bin subdirectory in the newly created Module directory. The path formed is C: / INETPUB / WWWROOT / MODULE / BIN. Copy MyModule.dll from your project's bin / debug directory to the C: / INETPUB / WWWROOT / MODULE / BIN directory. Follow the steps below to mark the new module directory as a web application:

Open Internet Service Manager. Right click on the Module directory and click Properties. On the Directory tab, click Create. Click OK to close the module properties dialog.

Back to top

Configuration system

In the c: / inetpub / wwwroot / module / directory, a file called web.config is created. Paste the following text into the web.config file:

Back to top

Test module

In the C: / INETPUB / WWWROOT / MODULE directory, create a .aspx file called Test.aspx. Paste the following text into the Test.aspx file: <% @ page language = "c #"%>

<% Response.write ("Hello from test.aspx.
");%> Create a global.asax file in a C: / INETPUB / WWWROOT / MODULE directory. Paste the following code into the global.asax file: <% @ import namespace = "mymodule"%>