How to Use Visual C #. NET to create an ASP. net http Module

Source: Internet
Author: User
Implementation Module
1. Create a Visual Studio. net c # class library project named MyModule.
2. Set a pairSystem. Web. dllAssembly reference.
3. Add the following command to the class:

            using System.Web;               

4. Rename the SyncModule. cs class and change the class definition accordingly.
5. ImplementationIHttpModuleInterface. Your class definition should be as follows:

            public class SyncModule : IHttpModule              

6. Decide what events you want to book. The following table summarizesHttpApplicationObject reservation event:
• AcquireRequestState: This event can be called to allow the module to obtain or create the Request status (for example, session ).
• AuthenticateRequest: This event is called when the security module needs to authenticate the user before processing the request.
• AuthorizeRequest: When a request requires authorization, the security module calls this event. This event is called after authentication.
• BeginRequest: Call this event to notify the module that a new request is about to begin.
• Disposed: Call this event to notify the module that the application is about to terminate for some reason. Allows the module to perform internal cleanup.
• EndRequest: Call this event to notify the module that the request is about to end.
• Error: This event is called to notify the module of an error during request processing.
• PostRequestHandlerExecute: Call this event to notify the module handler that the request has been processed.
• PreRequestHandlerExecute: Call this event to notify the module to call the processing program used to process the request.
• PreSendRequestContent: Call this event to notify the module that the request content will be sent to the client.
• PreSendRequestHeaders: Call this event to notify the module that an HTTP header will be sent to the client.
• ReleaseRequestState: Call this event to allow the module to release the status after the handler completes processing the request.
• ResolveRequestCache: This event is called after authentication. The cache module uses this event to determine whether the request should be processed by its cache or whether the handler should process the request.
• UpdateRequestCache: This event is called after the processing program responds. The cache module should update its cache based on the response.

This example usesBeginRequestEvent.

7. Follow these steps:IHttpModuleInterfaceInitAndDisposeMethod:

            public void Init(HttpApplication app)
            {
            app.BeginRequest += new EventHandler(OnBeginRequest);
            }
            public void Dispose(){ }

8. Create a delegate for the event as follows:

            public delegate void MyEventHandler(Object s, EventArgs e);             

9. DefineMyEventHandlerPrivate local variables to record reference to the event:

            private MyEventHandler _eventHandler = null;             

10. Create an event to mount the delegate to a method in the Global. asax file or fromHttpApplicationClass inherited by the object:

            public event MyEventHandler MyEvent
            {
            add { _eventHandler += value; }
            remove { _eventHandler -= value; }
            }

11. CreateOnBeginRequestMethod.HttpApplicationOfBeginRequestEvent:

            public void OnBeginRequest(Object s, EventArgs e)
            {
            HttpApplication app = s as HttpApplication;
            app.Context.Response.Write("Hello from OnBeginRequest in custom module.<br>");
            if(_eventHandler!=null)
            _eventHandler(this, null);
            }  

12. Compile the project.

 

Deployment module


1. Create a directory named Module under C: \ Inetpub \ Wwwroot.
2. Create a sub-directory named Bin under the newly created Module Directory. In this way, the path is C: \ Inetpub \ Wwwroot \ Module \ Bin.
3. Copy MyModule. dll from the Bin \ Debug directory of the project to the C: \ Inetpub \ Wwwroot \ Module \ Bin directory.
4. Follow these steps to mark the new Module directory as a Web application:

A. Open the Internet Service Manager.
B. Right-click the Module Directory, and then clickAttribute.
C. InDirectoryTab, clickCreate.
D. ClickOKTo closeModule attributesDialog box.

 

Configuration System


1. In the C: \ Inetpub \ Wwwroot \ Module \ directory, create a file named Web. config.
2. Paste the following text into the Web. config file:

            <configuration>
         <system.web>
        
         <add name="MyModule" type="MyModule.SyncModule, MyModule" />
         
         </system.web>
</configuration>

 

Test Module


1. In the C: \ Inetpub \ Wwwroot \ Module Directory, create a. aspx file named Test. aspx.
2. Paste the following text into the Test. aspx file:

            %@Page Language="C#"%
<% Response.Write("Hello from Test.aspx.<br>"); %> 

3. Create a Global. asax file in the C: \ Inetpub \ Wwwroot \ Module Directory.
4. Paste the following code into the Global. asax file: <%@ Import Namespace="MyModule" %>
            <script language="C#" runat=server >
            protected void MyModule_OnMyEvent(Object src, EventArgs e)
            {
            Context.Response.Write("Hello from MyModule_OnMyEvent called in Global.asax.<br>");
            }
            </script> 

   

5. Request the Test. aspx page. The following lines of text are displayed:

Hello from OnBeginRequest in custom module.            Hello from MyModule_OnMyEvent called in Global.asax.            Hello from Test.aspx.     

 

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.