Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Follow these steps to add an HTTP Module:</p> <ol> <li>Create a new Visual Studio .NET C# Class Library project named <code>MyModule</code>.</li> <li>Set a reference to the <code>System.Web.dll</code> assembly.</li> <li><p>Add the following directive to the class:</p> <pre><code>using System.Web; </code></pre></li> <li><p>Rename the class <code>SyncModule.cs</code>, and then change the class definition to reflect this.</p></li> <li><p>Implement the <code>IHttpModule</code> interface. Your class definition should appear as follows:</p> <pre><code>public class SyncModule : IHttpModule </code></pre></li> <li><p>Decide to which events you will subscribe. The following list outlines the available events from the <code>HttpApplication</code> object to which you can subscribe:</p> <ul> <li><code>AcquireRequestState</code>: Call this event to allow the module to acquire or create the state (for example, session) for the request.</li> <li><code>AuthenticateRequest</code>: Call this event when a security module needs to authenticate the user before it processes the request.</li> <li><code>AuthorizeRequest</code>: Call this event by a security module when the request needs to be authorized. Called after authentication.</li> <li><code>BeginRequest</code>: Call this event to notify a module that new request is beginning.</li> <li><code>Disposed</code>: Call this event to notify the module that the application is ending for some reason. Allows the module to perform internal cleanup.</li> <li><code>EndRequest</code>: Call this event to notify the module that the request is ending.</li> <li><code>Error</code>: Call this event to notify the module of an error that occurs during request processing.</li> <li><code>PostRequestHandlerExecute</code>: Call this event to notify the module that the handler has finished processing the request.</li> <li><code>PreRequestHandlerExecute</code>: Call this event to notify the module that the handler for the request is about to be called.</li> <li><code>PreSendRequestContent</code>: Call this event to notify the module that content is about to be sent to the client.</li> <li><code>PreSendRequestHeaders</code>: Call this event to notify the module that the HTTP headers are about to be sent to the client.</li> <li><code>ReleaseRequestState</code>: Call this event to allow the module to release state because the handler has finished processing the request.</li> <li><code>ResolveRequestCache</code>: Call this event after authentication. Caching modules use this event to determine if the request should be processed by its cache or if a handler should process the request.</li> <li><code>UpdateRequestCache</code>: Call this event after a response from the handler. Caching modules should update their cache with the response.</li> </ul></li> <li><p>Implement the Init and Dispose methods of the IHttpModule interface as follows:</p> <pre><code>public void Init(HttpApplication app) { app.BeginRequest += new EventHandler(OnBeginRequest); } public void Dispose(){ } </code></pre></li> <li><p>Create a delegate for an event as follows:</p> <pre><code>public delegate void MyEventHandler(Object s, EventArgs e); </code></pre></li> <li><p>Define a private local variable of the type <code>MyEventHandler</code> to hold a reference to the event:</p> <pre><code>private MyEventHandler _eventHandler = null; </code></pre></li> <li><p>Create an event that hooks up the delegate to the method in the Global.asax file or class that inherits from the <code>HttpApplication</code> object:</p> <pre><code>public event MyEventHandler MyEvent { add { _eventHandler += value; } remove { _eventHandler -= value; } } </code></pre></li> <li><p>Create the <code>OnBeginRequest</code> method, which hooks up to the <code>BeginRequest</code> event of <code>HttpApplication</code>:</p> <pre><code>public void OnBeginRequest(Object s, EventArgs e) { HttpApplication app = s as HttpApplication; app.Context.Response.Write("Hello from OnBeginRequest in custom module.&lt;br&gt;"); if(_eventHandler!=null) _eventHandler(this, null); } </code></pre></li> <li><p>Compile the project</p></li> </ol> <p>source: <a href="http://support.microsoft.com/kb/307996" rel="noreferrer">http://support.microsoft.com/kb/307996</a></p> <p>Adding an HTTP Module to your <code>web.config</code> will look something like the following:</p> <pre><code>&lt;system.web&gt; &lt;httpModules&gt; &lt;add name="CustomHttpModule" type="MyCustomHttpModule"/&gt; &lt;/httpModules&gt; &lt;/system.web&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload