Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

Search Engine Friendly URL’s Using Routing in ASP.Net 3.5
What is Routing?
Routing is a technique which enables us to use a descriptive,search engine and human friendly URL’s for ASP.Net application to access a resource.In this technique,the URL will not map to a resource physically.
Advantages of Routing
Most often, we work on data driven websites where data is categorized and stored.For example,if we are building a shopping website we may have products in various categories like home appliances,mobiles,Apparels, Jewels,Perfumes,etc.To build a list page to display the products in a category, we will normally develop an asp.net page that accepts the category ID as query string to populate the product list.
To understand the routing in ASP.Net, we will build a very simple asp.net application that displays list of employees in different departments.
Steps
  1. Open Visual Studio 2008.
  2. Click New> Website and Select "ASP.Net Web Site".You can select the language of your choice. Rename the website name as per your need.
  3. Include a new SQL express database inside App_Data folder and create a table called Employees and Department.
Note
You can add database by right clicking App_Data folder in the solution and clicking Add New Item.This will bring a dialog box where you need to select "Sql Server Database" and click Add.Then,create a table called Employees and Department with the necessary columns using the "Server Explorer".Just right click the added database and click “Open” to open your database using Server Explorer on the left pane of your Visual Studio 2008.
In this example,we will create a simple route that has the department id of the employees in the URL instead of passing it as query string.Refer below pattern,
The valid URL’s for the above routes may be,
Employees/Sales/list.aspx
Employees/HR/list.aspx
Employees/IT/edit.aspx
As we said earlier,we need to first create and register route to a route handler in Global.asax file.In order to register RouteHandler,we need to create the route handler to handle the request that has the url in the following route pattern
"Employees/{Dept}/{action}.aspx"
To do this,right click your solution in solution explorer and include a class file.We have named it as EmployeeRouteHandler.Import the routing namespace System.Web.Routing.Now,inherit the class from IRouteHandler interface and implement GetHttpHanlder() method, which should return the actual page or a handler to handle the request.Refer the code below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Routing;
/// <summary>
/// Summary description for EmployeeRouteHandler
/// </summary>
public class EmployeeRouteHandler: IRouteHandler
{    
  public EmployeeRouteHandler()
   {
      //
      // TODO: Add constructor logic here
      // public class ProductInfoService : System.Web.Services.WebService
   }
   public IHttpHandler GetHttpHandler(RequestContext requestContext)
   {
      string DeptName = requestContext.RouteData.Values["Dept"] as string;
      HttpContext context = HttpContext.Current;
      context.Items.Add("Dept", DeptName);
      string action = requestContext.RouteData.Values["action"] as string;
      if(action.ToLower() == "list")
          return BuildManager.CreateInstanceFromVirtualPath("~/Default.aspx",
          typeof(Page)) as Page;
      else
          return BuildManager.CreateInstanceFromVirtualPath("~/Default.aspx",
          typeof(Page)) as Page;
    }
}
In the above code,we have extracted the place holder value(Department id and action in our case) from the RouteData object.Refer the below line,
string DeptName = requestContext.RouteData.Values["Dept"] as string;
string action = requestContext.RouteData.Values["action"] as string;
Note
The Routing engine will process the incoming URL and will populate the values that are matching the placeholders and will make it available throughout the request.The actual Page object or the target page can be got by calling the CreateInstanceFromVirtualPath() method in BuildManager object.Next,we need to populate the RouteTable with all the possible Routes and RouteHandler object to process the incoming request to the application. To do this,we need to first add a Global.asax file into our solution. Right click the solution, and select “Add New Item”.In the dialog,select “Global Application Class” and click OK.Now,register all the Routes with their corresponding RouteHandler and populate it into the RouteTable object in Application_Start event.It is only "Employees/{Dept}/{action}.aspx" in our case.
Refer the code below,
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
   RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
   routes.Add("EmpOperation",new Route
   (
      "Employees/{Dept}/{action}.aspx",
      new EmployeeRouteHandler()
   )
   );
}
Job or extra money for students

Search Engine Rank of your blog or websites