How to add Header and Footer on every page by using HttpModule in asp.net
In this post,I will show you a very simple way to add header and footer on every page using HttpModule.
- Open visual studio and create a new website.
- Right click on website and add a new class named HeaderAndFooter,and inherit this class with IHttpModule inteface.
using System;
using System.Web;
using System.IO;
public class HeaderAndFooter : IHttpModule
{
const string PageHeaderText = "<h1>Header Added by Module<h1>";
const string PageFooterText = "<h1>Footer added by Module</h1>";
public void Init(HttpApplication app)
{
// Register for pipeline events
app.BeginRequest += new EventHandler(OnBeginRequest);
app.EndRequest += new EventHandler(OnEndRequest);
}
public void Dispose()
{
// Nothing to do here
}
public void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext ctx = app.Context;
// Add custom header to the HTTP response
ctx.Response.AppendHeader("Author", "DinoE");
ctx.Response.Write(PageHeaderText);
}
public void OnEndRequest(object sender, EventArgs e)
{
// Get access to the HTTP context
HttpApplication app = (HttpApplication)sender;
HttpContext ctx = app.Context;
// Append some custom text
ctx.Response.Write(PageFooterText);
}
}
- Open the web.config and register the module as below
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<httpModules>
<add name="HeaderAndFooter" type="HeaderAndFooter,App_Code"/>
</httpModules>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
0 comments: