It is becoming extremely important now a days to use simple URLs without showing the .aspx extention and also hiding the Query String parameters such as page.aspx?name1=value1&name2=value2. SEO experts swear that simple canonical urls such as mysite.com/CountryName/CityName/CompanyName/ are much better in terms of search engine friendliness than mysite.com/getlisting.aspx?Country=CountryName&City=CityName&Company=CompanyName
We can clearly see that the former URL is much better even for human beings as they are easy to remember and also show that the site is well structured, which is very important for huge sites that contain tens of thousands of pages or page variations.
In this post I am going to show you how to use ASP.NET Routes to map simple canonical form URLs to aspx pages and pass in parameters using ASP.NET 4.0 Routing functionality.
Simple ASPX Page accepting parameters thru QueryString
I am going to develop a simple ASP.NET page called LearnRouting.aspx which takes only one GET parameter called student and return the age of the student in the response. LearnRouting.aspx?student=Jim will show you the age of the student whose name is Jim. The ASPX page itself is very simple and the code behind simply fetches the age of the given student and assigns to a variable.
The LearnRouting.aspx page is very simple and uses a master page, hence in the asp:Content block for MainContent ID I am going to simply display the value of the string variable strStudentAge.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="LearnRouting.aspx.cs" Inherits="WebApplication1.WebForm1" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <%= strStudentAge%></asp:Content>
The code behind C# program is listed below. It simply fetches the GET parameter called student from the Query String. Then the age is fetched by calling the GetAge(student) function in the Page_Load () method and simply assigns the name and age to a global string variable called strStudentAge which is displayed in the MainContent ID of the ASPX page.
namespace WebApplication1 {
public partial class WebForm1 : System.Web.UI.Page {
public string strStudentAge = "";
protected void Page_Load(object sender, EventArgs e) {
string studentname = Request.QueryString["student"];
strStudentAge = "Age of student " + studentname + " is: "+GetAge(studentname);
}
public string GetAge(string x) {
if (x == "Jim") return "10";
if (x == "Tom") return "11";
if (x == "Jane") return "12";
return "Unknown";
}
}
}
The output will look as follows:
Nothing fancy so far. Very simple ASPX application that shows the student age, given the student name in the query string.
Calling the above page using ASP.NET Routing
Now I’d like to modify this ASPX page a little bit and by using ASP.NET 4.0 Routing, I’d like to route all URL patterns such as /Age/StudentName to automatically map to this LearnRouting.aspx page and pass in the second token StudentName as the input parameter. In otherwords, I’d like to map
mysite.com/Age/StudentName to mysite.com/LearnRouting.aspx?student=StudentName
Step 1
To achieve this I’ll use the ASP.NET Class RouteTable and its static member Routes and call MapPageRoute() method on it in the Application_Start() method of my web application i.e., in my global.asax page.
void Application_Start(object sender, EventArgs e) {
// Code that runs on application startup
RouteTable.Routes.MapPageRoute("Route1_StudentAge", "Age/{studentname}", "~/LearnRouting.aspx");
}
System.Web.Routing namespace provides classes that are used with URL routing, which enables you to use URLs that do not map to a physical file. The RoutTable is a class and Routes is a static member of this class, so it can be accessed from the Application_Start() method of my application. The Routes is a member of this RouteTable and is of type RouteCollection. By calling MapPageRoute, we can add a simple route to this collection. In my case, I want to add a Page Route whose name is Route1_StudentAge and it maps Age/{studentname} to LearnRouting.aspx page. Notice the studentname in curly brackets {…}. This indicates that studentname is a variable where as Age is a constant in the URL. So all URLs that match this pattern will now get mapped to LearnRouting.aspx page.
Step 2
Now both mysite.com/Age/Jim and mysite.com/Age/Jane will map to and call the same ASPX page, so in side the ASPX page we will need modify the code to fetch the studentname and respond accordingly.
protected void Page_Load(object sender, EventArgs e) {
string student_name = Request.QueryString["student"];
if (student_name==null) student_name = RouteData.Values["studentname"] as string;
strStudentAge = "Age of student " + student_name + " is: "+GetAge(student_name);
}
Notice the if statement. I am fetching the GET variable from the QueryString just as I did before so that I will be compatible to requests that directly call the aspx page with the studentname in the QueryString. In order to respond to the new format URLs that do not have the aspx or QueryString in them, I will need to make use of Page.RouteData object. This object contains a member Values which is a RouteValueDictionary, and one of the key/value pairs in this dictionary is studentname, which was defined in my global.asax file inside the curly brackets {…}. Once I can access this studentname that was passed in as part of the new URL, I can simply do what I did before, i.e., call the GetAge() function and assign the returned string to my global variable strStudentAge. Rest of the code remains unchanged.
Now I fireup the browser and call my server with the URL localhost/Age/Jim and as you can see below it simply returns the same output as before.
Advantages of using Routes in ASP.NET
- You can simplify the site structure by using canonical form of URLs
- You can hide the aspx extention, ‘?’ and ‘&’ characters in the query string
- Helps you in SEO thru the refined site structure
- Since the URL structure is simplified, the application is easily portable to a non ASP platform as the URLs don’t have aspx in them.
Conclusion
- ASP.NET Routes can be used to map URLs to aspx pages and pass parameters.
- The input to the aspx page is from the URL pattern as opposed to the querystring.
- In the global.asax page, in the Application_Start() method, you will simply do the mapping from a canonical URL to an aspx page using RouteTable.Routes.MapPageRoute(mapping_name, URL_Pattern, target_aspx)
- Instead of using Request.QueryString object, you will need to use RouteData.Values[] collection object to fetch the parameters passed in.












I hope to implement this on my accommodation Web site shortly, and found your post to be a good introduction. Thanks.
Simply superb