An Http Module is an assembly that implements IHttpModule interface and serves as a component that can be plugged into ASP.NET request processing pipeline. This is performed by registering for a set of events.
To implement IHttpModule, you need to implement its two methods; Init and Dispose. Here is a couple of events the module can register for. You hook up to those events in Init method.
- AcquireRequestState
- AuthenticateRequest
- AuthorizeRequest
- BeginRequest
- Disposed
- EndRequest
- Error
Before I dive into the details, I should mention something I experienced in IIS 7. When implementing HttpModule, the website must use an application pool with managed pipeline mode of Classic. If integrated mode is chosen, the code in the module will not be hit.
To implement an HttpModule, use IHttpModule interface and implement Dispose and Init methods in your module class (in the example, Module1). In Init method which takes in an HttpApplication object, you can register handlers for several events.
public class Module1 : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
app.Context.Response.Output.Write("This is the beginning.<br/>");
}
}
Then in the web.config file, you need to add the code below to register the module:
<httpModules>
<add name="Module1" type="Module1"/>
</httpModules>
A good example of an HttpModule could be Url Rewriting:
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string url = app.Request.Url.ToString();
if (url.Contains("product1.aspx"))
{
app.Context.RewritePath("det.aspx?name=product1");
}
else if(url.Contains("product2.aspx"))
{
app.Context.RewritePath("det.aspx?name=product2");
}
}
Here arises a small problem. In this example, if you have a button in det.aspx page, by clicking it, a postback occurs which changes the Url to the rewritten path. To avoid this, you will need to create a class library and implement a class that derives from System.Web.UI.HtmlControls.HtmlForm:
namespace ActionlessForm
{
public class Form :System.Web.UI.HtmlControls.HtmlForm
{
protected override void RenderAttributes(System.Web.UI.HtmlTextWriter writer)
{
writer.WriteAttribute("name", this.Name);
base.Attributes.Remove("name");
writer.WriteAttribute("method", this.Name);
base.Attributes.Remove("method");
this.Attributes.Render(writer);
base.Attributes.Remove("action");
if (base.ID != null)
{
writer.WriteAttribute("id", base.ClientID);
}
}
}
}
You need to add a reference to the new class library in the website and add a Register directive to det.aspx page:
<%@ Register TagPrefix="mnf" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
Then you will need to set a prefix for the page’s form control as follows:
<mnf:form id="form1" runat="server">
…
</mnf:form>
No comments:
Post a Comment