Asp.Net URL Rewriting using Request.PathInfo C# VB.Net

To publish cleaner URL in your Asp.Net application you can use URL Rewriting. The most popular reason for URL Rewriting is let you have implemented pages based on query string like http://yourdomain/product.aspx?Brand=Lux. But now you want to change the URL to http://yourdomain/product.aspx/Lux for SEO purpose. There is a lot of way to implement this. One of the easy way you can achieve URL Rewriting by using Request.PathInfo of Asp.Net. Mind it this is not the best way for a large site because this technique requires some sort of coding which may be error prone. One another thing that if you have 10 brands & you use query string to populate Brand related product then google will index only one URL but if you use URL like http://yourdomain/product.aspx/Lux then google will treat all 10 as individual pages. Nice thing am i right? Another reason for URL Rewriting using Request.PathInfo is, you can support query string based URL that already bookmarked by old user’s.

Objective/Outcome:
Asp.Net URL Rewriting using Request.PathInfo Csharp VB.Net

Now add Two LinkButton in Default.aspx Page:

    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Go to Ronaldo Page Using Request.PathInfo</asp:LinkButton><br />
    <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click" >Go to Ronaldo Page using Query String</asp:LinkButton>

Now implement Two Click Event Like:
C# Code:

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("About.aspx/Ronaldo");
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect("About.aspx?Player=Ronaldo");
    }

Note: Check both Response.Redirect() method. First one is more SEO friendly than second one.

Now in About page how to implement Post Call:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Response.Write("Player=" + GetPlayer());
            // Now you can do anything since you got the Player Name
        }
    }

    public string GetPlayer()
    {
        if (Request.PathInfo.Length == 0)
            return Request["Player"];
        else
            return Request.PathInfo.Substring(1);
    }

Note: If Request.PathInfo.Length =0 then we will try with query string for backward compatibility.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ' Now you can do anything since you got the Player Name
            Response.Write("Player=" & GetPlayer())
        End If
    End Sub

    Public Function GetPlayer() As String
        If Request.PathInfo.Length = 0 Then
            Return Request("Player")
        Else
            Return Request.PathInfo.Substring(1)
        End If
    End Function

Note: If Request.PathInfo.Length =0 then we will try with query string for backward compatibility.

Good to Know:
http://weblogs.asp.net/scottgu/tip-trick-url-rewriting-with-asp-net

Recommended third Party Tool:
http://urlrewriter.net/

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*