URL Rewriting Using Database and IHttpModule in IIS7/8

Step-1: In database table keep a field named “ProfilePageTitle” for rewrite URL

SELECT [SystemId]
FROM tblUserSetup

Step-2: In your project add “IHttpModule” i.e. FixURLs.cs

public class FixURLs : IHttpModule

Step-3: add code to FixURLs.cs

#region IHttpModule Members
public void Dispose()
// do nothing
public void Init(HttpApplication context)
context.BeginRequest += new EventHandler(context_BeginRequest);

void context_BeginRequest(object sender, EventArgs e)
String qs = ” SELECT UserId, ProfilePageTitle FROM tblUserSetup WHERE UserId <> ‘A786’ “;
DataTable dt = new DataTable();
dt = ClsCommon.GetAdhocResult(qs).Tables[0];
catch (Exception ex) { }
HttpApplication app = (HttpApplication)sender;
for (Int32 i = 0; i < dt.Rows.Count; i++)
if (app.Request.RawUrl.ToLower().Contains(“/” + dt.Rows[i][“ProfilePageTitle”].ToString()))
app.Context.RewritePath(“~/frmFacultyInfoDetails.aspx”, “”, “qs=faculty,info,” + dt.Rows[i][“UserId”].ToString());

Step-4: .aspx and .aspx.cs code:

<asp:GridView ID="grvFacultyList" Width="650px" runat="server" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Size="14px"
Font-Underline="False" AutoGenerateColumns="False" ShowHeader="False" ForeColor="Black"
GridLines="Horizontal" OnRowDataBound="grvFacultyList_DataBound">
<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Center" />
<asp:Label ID="lblDesignation" runat="server" Text='<% #Eval("Designation") %>' Visible="false" />
(<%#Container.DataItemIndex+1 %>)
<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Left" />
<%--<asp:HyperLink ID="hlUserName" runat="server" Text='<% #Eval("UserName") %>' target="_blank"></asp:HyperLink>--%>
<asp:HyperLink ID="hlUserName" runat="server" Text='<% #Eval("UserName") %>' target="_blank"></asp:HyperLink>
<asp:Label ID="lblUserName" runat="server" Text='<% #Eval("UserName") %>' Visible="false" />
<br />
<asp:Label ID="lblEmail" runat="server" Text='<% #Eval("Email") %>' />
<br />
<asp:HyperLink ID="hlPersonalHomePage" runat="server" NavigateUrl='<% #Eval("Url") %>'
Text='<% #Eval("Url") %>' target="_blank" Visible="true"></asp:HyperLink>
<%--<asp:LinkButton ID="lbUrl" runat="server" Text='<% #Eval("Url") %>'></asp:LinkButton>--%>
<asp:Label ID="lblUrl" runat="server" Text='<% #Eval("Url") %>' Visible="false" />
<asp:Label ID="lblUserId" runat="server" Text='<% #Eval("UserId") %>' Visible="false" />
<asp:Label ID="lblProfilePageTitle" runat="server" Text='<% #Eval("ProfilePageTitle") %>' Visible="false" />
<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Center" />
<asp:Image ID="ImageUserId" Width="80px" Height="100px" runat="server" ImageUrl='<%#"HandlerFacultyImage.ashx?qsId=" + Eval("Id") %>' />
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="White" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />

protected void grvFacultyList_DataBound(object sender, GridViewRowEventArgs e)
foreach (GridViewRow gvr in grvFacultyList.Rows)
HyperLink hlUserName = gvr.FindControl(“hlUserName”) as HyperLink;
Label lblUserName = gvr.FindControl(“lblUserName”) as Label;
Label lblUserId = gvr.FindControl(“lblUserId”) as Label;

Label lblProfilePageTitle = gvr.FindControl(“lblProfilePageTitle”) as Label;

HyperLink hlPersonalHomePage = gvr.FindControl(“hlPersonalHomePage”) as HyperLink;
hlUserName.NavigateUrl = “~/” + lblProfilePageTitle.Text.ToString().Trim();

Step-5: IIS setup and Module adding:

i. Open Internet Information Service (IIS) Manager
ii. Select application and click the tab “features view”
iii. Modules–> add managed modules… –> in Type: field select the Module “FixUrls” Local Module.
Step-6: web.config
<add name="FixURLs" type="cse.FixURLs, cse" />
<modules runAllManagedModulesForAllRequests="true">
<add name="cse.FixURL" type="cse.FixURLs" preCondition="managedHandler" />
<add name="FixUrl" type="FixURLs" preCondition="managedHandler" />


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s