Sunday, November 13, 2011

URL Rewritting in asp.net

Here we will discuss about URL rewriting in asp.net. Though MVC has built in facility of URL rewritting but classic asp.net does not have.

Why it is necessary?
A SEO user friendly URL is necessary for the SEO purpose.
For example:
http://www.fewlines4biju.com/Blog.aspx?BlogID=5 URL is not a SEO user friendly URL.
http://www.fewlines4biju.com/Blogs/Details/another-article-by-bijay-kumar-for-seo-5.aspx , but this URL is a SEO friendly URL. So it is always better to have a URL like the above.

So we will discuss the steps below:
First of all we will check the web.config entries, I am writting the entries with the parent tag. You have to replace your folder structure accordingly.
web.config:
<configuration>
<configSections>
<section name="rewriter" requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
</configSections>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule"/>
</modules>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
<rewriter>
<rewrite url="~/Articles/Details/(.+)-(.+).aspx" to="~/Articles/article-details.aspx?ArticleID=$2"/>
</rewriter>
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
</httpModules>
</configuration>

Now we will check in the page to generate the URL:
I will tell you in the aspx page code inside the gridview.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
   <Columns>
        <asp:TemplateField>
            <ItemTemplate>
<asp:HyperLink ID="hylnkTitle" NavigateUrl='<%#GenerateURL(DataBinder.Eval(Container.DataItem,"Title"),DataBinder.Eval

(Container.DataItem,"TopicID"))%>'
                        runat="server" Text='<%#Eval("Title") %>'></asp:HyperLink>
</ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And now the code file:


 public static string GenerateURL(object Title, object strId)
    {
        string strTitle = Title.ToString();      
        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 = "http://www.fewlines4biju/Blogs/Details/" + strTitle + "-" + strId + ".aspx";

        return strTitle;
    }
Now our task is done, but...
for post back purpose, we have to do some other things.
Open the rar file URL Rewitting Code.rar and put the App_Browsers folder in the project root and Form.cs file in App_Code folder. Then it will work.
Full code can be downloaded here !!!

You can check this URL for more information.