URL Rewriting Using Database and IHttpModule in IIS7/8

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

SELECT [SystemId]
,[UserId]
,[RoleId]
,[UserName]
,[ProfilePageTitle]
,[UserPicture]
,[Designation]
,[Email]
,[LoginId]
,[Password]
,[PasswordHints]
,[HeadStatus]
,[OnLeave]
,[DisplayOrder]
,[ErrorCounter]
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);
}
#endregion

void context_BeginRequest(object sender, EventArgs e)
{
String qs = ” SELECT UserId, ProfilePageTitle FROM tblUserSetup WHERE UserId <> ‘A786’ “;
DataTable dt = new DataTable();
try
{
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">
<Columns>
<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblDesignation" runat="server" Text='<% #Eval("Designation") %>' Visible="false" />
(<%#Container.DataItemIndex+1 %>)
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<%--<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" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Image ID="ImageUserId" Width="80px" Height="100px" runat="server" ImageUrl='<%#"HandlerFacultyImage.ashx?qsId=" + Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<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" />
</asp:GridView>

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
<configuration>
<system.web>
<httpModules>
<add name="FixURLs" type="cse.FixURLs, cse" />
</httpModules>
<system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="cse.FixURL" type="cse.FixURLs" preCondition="managedHandler" />
<add name="FixUrl" type="FixURLs" preCondition="managedHandler" />
</modules>
</system.webServer>
<configuration>

Advertisements

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