Çok basit bir HttpModule örneğini sizlerle paylaşmak isterim.
AÅŸağıdaki kodu yeni bir Class Library projesi oluÅŸturarak, projeye ekleyeceÄŸiniz bir class dosyasına ekleyiniz. Bu projenizin isminin “MyHttpModuleProject” olduÄŸunu var sayıyorum.
using System;
using System.Web;
namespace Sunali
{
public class MyHttpModuleClass : IHttpModule
{
#region CTor
public MyHttpModuleClass()
{
}
#endregion
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(app_BeginRequest);
app.Error += new EventHandler(app_Error);
// Burada HttpApplication class'ının sahip olduğu tüm event'leri tanımlayabilirsiniz.
}
#endregion
#region Event Handlers
void app_BeginRequest(object sender, EventArgs e)
{
HttpApplication m_App = (HttpApplication)sender;
HttpContext m_Context = m_App.Context;
// Örneğin aşağıdaki satır web siteniz üzerindeki tüm sayfalara yazdırılır.
m_Context.Response.Write(string.Format("Mevcut URL: {0}<br /><br />", m_Context.Request.Url));
}
void app_Error(object sender, EventArgs e)
{
HttpApplication m_App = (HttpApplication)sender;
Exception exc = m_App.Server.GetLastError();
// exc deÄŸiÅŸkenine atanan Exception objesini istediÄŸiniz ÅŸekilde loglayabilirsiniz.
}
#endregion
}
}
Ayrıca web sitenizin web.config dosyasına da aşağıdaki ayarı eklemeniz gerekmektedir.
<?xml version="1.0"?>
<configuration>
<system.web>
<httpModules>
<add name="MyModule" type="Sunali.MyHttpModuleClass,MyHttpModuleProject" />
</httpModules>
</system.web>
</configuration>