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>

Managed Fusion URL Rewriter and Reverse Proxy

http://managedfusion.com/products/url-rewriter/

If you find some value in this project, and it has saved you time or money (lets be honest time is money), please consider donating a fraction of what you have saved to help fund the free development put forth by our developers. You can donate by sending money via PayPal to nberardi@gmail.com

About

Managed Fusion URL Rewriter is a powerful URL manipulation engine based on the Apache mod_rewrite extension. It is designed, from the ground up to bring all the features of Apache mod_rewrite to IIS 6.0 and IIS 7.0. Managed Fusion Url Rewriter works with ASP.NET on Microsoft’s Internet Information Server (IIS) 6.0 and Mono XPS Server and is fully supported, for all languages, in IIS 7.0, including ASP.NET and PHP. Managed Fusion Url Rewriter gives you the freedom to go beyond the standard URL schemes and develop your own scheme.

URL Rewriter provides web site owners with the ability to replace URL paths and querystring into links that your users can easily remember without the need for bookmarks. As an added benefit your web site becomes search engine friendly, which means higher page ranking from the search engines, and should result in better lead generation.

Advantages

  • Developed by my company but unlimited FREE use for everybody.
  • Full .NET 2.0, 3.0, and 3.5 support.
  • Full support for IIS 6.0 and IIS 7.0 (including integrated pipelines).
  • Fully functional Proxy and Reverse Proxy integrated in at no extra cost.
  • Full support for Mono XPS, and the integrated Visual Studio Web Development Server, two things that ISAPI_Rewrite and Ionic Rewriter cannot claim.
  • Create short URLs that are easy for your users to remember.
  • Structure your site for search engine readability.
  • Hide the implementation of your URL from the URL that is displayed to the user.
  • Provides easy implementation for standardizing your web site to help in SEO efforts.
  • Block hot linking of your sites content, very useful for sites that steal your images and consume your bandwidth for their gain.
  • Proxy content of one site into directory on another site.
  • Create a gateway server that brings together all your companies proprietary web application under one standardized schema through the proxy feature.
  • Create dynamic host-header based sites using a single physical site.
  • Change your ASP.NET file extensions to other extensions (i.e. .html). This also helps in migrating old CGI technology to ASP.NET, without changing your URL structure.
  • Return a browser-dependent content even for static files.
  • Reverse proxy content from behind your firewall on to the internet, without exposing your machine to the internet.
  • Ability to use the Reverse Proxy to make cross site AJAX requests.

How does it work?

Managed Fusion Url Rewriter is an HttpModule for the Microsoft ASP.NET Web Framework, or IIS 7.0 Integrated Pipeline. All rules are managed in a plain text using Apache mod_rewrite syntax.

These rules provide the web request a way to validate how the requesting friendly URL should be handled. Managed Fusion Url Rewriter does its best to find a matching rule for the friendly URL, if found, the request is rewritten to something your application understands or an HTTP redirect, to a different url, is sent back to the client depending on the actions given to the rule.

The result is a friendly and clean URL that completely masks your file structure from the client.

Unlike other existing URL rewriting process, Managed Fusion Url Rewrite is entirely masking the old URL so ASP.NET Form Postbacks are fully supported by this component and it uses the common Apache mod_rewrite syntax that is so popular with PHP, Cold Fusion, and Ruby.