Simple url rewriting ASP net with URLRewriter.Net
URL Rewriting with URLRewriter.Net
URL Rewriting has lots of benefits, listing its main benefits
- SEO Friendly URL
- Secured URL
- No need to change bookmark with change in site structure.
Before URL Rewriting my URL looks like
http://localhost:2661/URLRewrite2/DynamicPage.aspx?MyTitleId=1
After URL Rewriting URL is changed to
http://localhost:2661/URLRewrite2/Article/Asp-Net-website-paths-1.aspx
Step-by-Step Explanation
Step 1: Download Binary Files for URLRewriter.Net
Step 2: Add Reference to Binary Files, Right click project "Add Reference" and add binary files.
Step 3: Update Web.Config File to make URLRewriter.Net works.
<configuration> <configSections> <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" /> </configSections> <system.web> <httpModules> <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" /> </httpModules> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"> <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" /> </modules> <validation validateIntegratedModeConfiguration="false" /> </system.webServer> <rewriter> <rewrite url="~/Article/(.+)-(.+).aspx" to="~/DynamicPage.aspx?MyTitleId=$2"/> </rewriter> </configuration>
Step 4: Adding Function to Generate SEO Friendly URL from given Title
public static string GenerateURL(object Title, object strId) { string strTitle = Title.ToString(); #region Generate SEO Friendly URL based on Title //Trim Start and End Spaces. strTitle = strTitle.Trim(); //Trim "-" Hyphen strTitle = strTitle.Trim('-'); strTitle = strTitle.ToLower(); char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray(); strTitle = strTitle.Replace("c#", "C-Sharp"); strTitle = strTitle.Replace("vb.net", "VB-Net"); strTitle = strTitle.Replace("asp.net", "Asp-Net"); //Replace . with - hyphen strTitle = strTitle.Replace(".", "-"); //Replace Special-Characters for (int i = 0; i < chars.Length; i++) { string strChar = chars.GetValue(i).ToString(); if (strTitle.Contains(strChar)) { strTitle = strTitle.Replace(strChar, string.Empty); } } //Replace all spaces with one "-" hyphen strTitle = strTitle.Replace(" ", "-"); //Replace multiple "-" hyphen with single "-" hyphen. strTitle = strTitle.Replace("--", "-"); strTitle = strTitle.Replace("---", "-"); strTitle = strTitle.Replace("----", "-"); strTitle = strTitle.Replace("-----", "-"); strTitle = strTitle.Replace("----", "-"); strTitle = strTitle.Replace("---", "-"); strTitle = strTitle.Replace("--", "-"); //Run the code again... //Trim Start and End Spaces. strTitle = strTitle.Trim(); //Trim "-" Hyphen strTitle = strTitle.Trim('-'); #endregion //Append ID at the end of SEO Friendly URL strTitle = "~/Article/" + strTitle + "-" + strId + ".aspx"; return strTitle; }
Step 5: Changing DataBinder.Eval Function in .Aspx Page to reflect changes in URL of Grid.
Note: Learn more about DataBinder.Eval Function
<%#GenerateURL(DataBinder.Eval(Container.DataItem,"Title"),DataBinder.Eval(Container.DataItem,"Id"))%>
Sources :