Tuesday, 4 December 2012

.Net Tips , C# Tips : Change or Altering the processed output of an ASP.NET web page by Implementing IHttpModule interface with C# Examples and VB.Net Examples and Sample

Click to Download C# Example ImplementIHTTPModuleInterfaceInCSharp.zip
Click to Download VB.NET Example ImplementIHTTPModuleInterfaceInVBNET.zip

By Implementing IHttpModule interface we can plug classes into the request-processing pipeline. HTTP Module class do this by hooking into a application events as it processes the HTTP request.
To create an HttpModule, we simply create a class that implement the System.Web.IHttpModule interface.This interface requires you to implement Init and Dispose methods.

The Init method is the basic primary method. In this method we implement HttpModule functionality. You can register different events in to the Init method. Init method has a single parameter it's an object of HttpApplication class and it's name is "context".

To use this module, we must include the module in the request processing pipeline so that ASP.NET know that. We can do this to modify the web.config file.
For that you have to add an <httpModules> section to your web.config file.

Here is the generic format :
The generic format of the <httpModules> section is
        <add name="[modulename]" type="[namespace.classname, assemblyname]" />

If you want to deploy your application to an IIS 7 server, you must also add the module configuration to the
<system.webServer> configuration section.

    <add name="[modulename]" type="[namespace.classname, assemblyname]" />

If we create our HttpModule in the App_Code directory of an ASP.NET Web site. For that we use the text “App_Code” as the assembly name, which tells ASP.NET that  module is located in the dynamically created assembly.

If we create HttpModules as a separate class library, in this case we use the assembly name of the library.

Here is example for this.

In this example we modify the HTTP output stream before it is sent to the client. This is very useful and simple tool if you want to add text to each page from your Web site, like a date/time stamp or the server name that processed the request, By doing this you do not modify each individual page in your application.

After creating class and register that module in web.config file execute your application and view any webpage you notice that at the end of content of web page there is one "Request Process on date/time" text. In the output you can view that source of generated HTML file.

Web.Confing Setting : 
            <add name="SimpleModule" type="SimpleModule, App_code"/>

ASPX Code :
    <form id="form1" runat="server">

C# Examples :
public class SimpleModule : IHttpModule
    private HttpApplication objApplication = null;
    public void Dispose()
    public void Init(System.Web.HttpApplication context)
        objApplication = context;
        context.EndRequest += new EventHandler(context_EndRequest);
    void context_EndRequest(object sender, EventArgs e)
        string message =
        string.Format("<br/>Request Processed on {0}", System.DateTime.Now.ToString());

VB.net Examples :
Public Class SimpleModule
    Implements IHttpModule

    Dim WithEvents objApplication As HttpApplication = Nothing

    Public Overridable Sub Init(ByVal context As HttpApplication) _
    Implements IHttpModule.Init
        objApplication = context
    End Sub

    Public Overridable Sub Dispose() Implements IHttpModule.Dispose
    End Sub

    Public Sub context_EndRequest(ByVal sender As Object, ByVal e As EventArgs) Handles objApplication.EndRequest
        Dim message As String =
        String.Format("<br/>Request Processed on {0}", System.DateTime.Now.ToString())
    End Sub

End Class

Output :
Implement IHttpModule Interface


Output (HTML Source) :
Implement IHttpModule Interface
(To view original image , click on image)

No comments:

Post a Comment