Search This Blog

Wednesday, November 27, 2013

How to add Header and Footer on every page by using HttpModule in asp.net

Open visual studio and create a new website.
Right click on website and add a new class named PageFooterandHeader and inherit this class with IHttpModule inteface.


using System;
using System.Web;


public class PageFooterandHeader : IHttpModule
{
    const string PageHeaderString = "Page Header with the help of HttpModule";
    const string PageFooterString = "Page Footer with the help of HttpModule";

    public void Init(HttpApplication httpapp)
    {
        httpapp.BeginRequest += new EventHandler(httpapp_BeginRequest);
        httpapp.EndRequest += new EventHandler(httpapp_EndRequest);
    }

    void httpapp_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpContext context = app.Context;
      
        context.Response.Write(PageHeaderString);
    }

    void httpapp_EndRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpContext context = app.Context;

        context.Response.Write(PageFooterString);
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}


·         Open the web.config and register the module as below




<?xml version="1.0"?>

<configuration>
  <system.web>
    <httpModules>
<add name="PageFooterandHeader" type="PageFooterandHeader,App_Code"/>
    </httpModules>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>


Final Output:
Post a Comment

Popular Posts