Tuesday 10 December 2013

SEO-friendly URLs in .NET

Clean URLs, User-friendly URLs or SEO-friendly URLs are purely structural URLS that do not contain a query string [e.g., action=delete&id=91] and instead contain only the path of the resource.
Unclean URL
SEO-friendly URL
http://example.com/index.aspx?page=foo
http://example.com/foo
http://example.com/products.aspx?id=1&cat=Apple
http://example.com/store/1/Apple

The most often cited reasons for using clean URLs is for search engine optimization, but clean URLs can also greatly improve usability and accessibility. Removing unnecessary parts simplifies URLs and makes them easier to type and remember.

In this article, I am going to explain how to create SEO-friendly URLs.

Step 1 - Create a new ASP.NET 4.0 Web Application Project (WAP)


Start an instance of Visual Studio 2010 and create a new WAP project by going to
File > New >  Project > Web > ASP.NET Web Application. During this article, I will create Web.config, Global.asax &  two.aspx files it as shown below:





















Step 2 – Open Index.aspx page and add the following code:


<body>
  <form id="FRM_LIST" runat="server">
  <asp:HyperLink runat="server" NavigateUrl="/Store/1/Apple" Text="Apple" />
  <br />
  <!--Below link RoutURLExpressionBuilder generate the URL dynamically-->
  <asp:HyperLink runat="server"
    NavigateUrl="<%$RouteUrl:RouteName=MyRouteName, id=2, name=Samsung %>"
    Text="Samsung" />
  </form>
</body>

Step 3 – Open Product.aspx page and add the following code:


<body>
  <form id="FRM_PRODUCT" runat="server">
  <asp:Label ID="LBL_PRODUCT_NAME" runat="server"></asp:Label>
  <asp:Label ID="LBL_PRODUCT_ID" runat="server"></asp:Label><br />
  </form>
</body>

Step 4 – Open Product.aspx.cs page and add the following code:


protected void Page_Load(object sender, EventArgs e)
{
  int PRODUCT_ID = Convert.ToInt32(Page.RouteData.Values["id"]);
  string PRODUCT_NAME = Page.RouteData.Values["Name"].ToString();
  LBL_PRODUCT_NAME.Text = PRODUCT_NAME;
  LBL_PRODUCT_ID.Text = "Product Id - " + PRODUCT_ID;
}

Step 5 – Open Global.asax page and add the following code:


void Application_Start(object sender, EventArgs e)
{
  RouteTable.Routes.MapPageRoute(
    "MyRouteName",            // Route Name
    "Store/{id}/{Name}",      // Url and Parameters
    "~/Product.aspx"          // Page Handling Request
    );
}

Now select Index.aspx and hit Ctrl+F5 then you should see the below page:














On clicking the “Apple” link you should be able to see the below page with SEO-friendly URL.














About Author:
Satish More is a consultant in Systems Plus Pvt. Ltd. Within Systems Plus, he actively contributes to the areas of Technology and Information Security. He can be contacted at satish.m@spluspl.com

No comments:

Post a Comment