URL Rewriting in ASP.NET with Global.asax


This article is to describe URL Rewriting in ASP.NET using Global.asax using the Simplest Language and easiest method with Screenshots. Go through and get a completely SEO Friendly website using URL re-writing in just two block of Codes.

Hi Once again, I am back with a new article. This article is about URL Re-writing. Do you think URL rewriting is much easier in PHP and somehow difficult in ASP.NET.

What We will be using:


1. Visual Studio for Writing Code and Compiling
2. Global.asax for Handling request.
3. System.Web.Routing Namespace
4. C# for Backend Coding (Can also use VB)

Objective:


Let's clear the scenario.

Suppose you do not want to Display the .aspx extension of your webpage. Or you may rename a webpage and make it SEO friendly.

SEO (Search Engine Optimization) is the world that all developer, designer and personality related with Web is talking about. In simple word, it is the process to make your website user friendly and easy to reach for Public including Search Engine.

Suppose your webpage is using Querystring to fetch data from server and display to front-end, then your URL looks like below.

http://www.yourname.com/default.aspx?PageID=120 or
http://www.yourname.com/default.aspx?PageID=120&CatID=200&Season=Winter or some more complicated.

Such url's are better with Applications, because we need to mantain security, but for Public websites, it is not Good to find in search engine and remember.

User can easily remember following types of URL
http://www.yourname.com/Profile
http://www.yourname.com/winter/training/aspnet

Or some more better.


We will achieve the above goal using simple concept here.

Changes in Global.asax



First Import and Namespace called System.Web.Routing.

<%@ Import Namespace="System.Web.Routing" %>


Then create a method as below:

public static void RegisterRoutes(RouteCollection rColl)
{
rColl.MapPageRoute("Default", "Pages/{PageName}", "~/Default.aspx");
}


Description of Above Code:



rColl is variable of type RouteCollection which is a class inside System.Web.Route.
We are mapping Routes and Route name is Default, this will output all pages like Pages/110 or whatever our Query string is. Then Default.aspx is virtual physical path of file, which is doing the job.

And Call the above created method inside Application_Start section of Global.asax.

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}


Now, move to Default.aspx and apply above functionality.

Move to Backend of Default.aspx (Default.aspx.cs) and code the below lines in Page_Load section after importing System.Web.Routing.

protected void Page_Load(object sender, EventArgs e)
{
string pageName = Page.RouteData.Values["PageName"].ToString();
Response.Write("<h1>" + pageName + "</h1>");
this.Title = pageName;
}


See below Screenshots for Output.

Rewite Ex1

Rewite Ex2

Working source code is added at the Bottom of article. Waiting for responses on this article.

All the Best.

Glad to be,
John Bhatt
P.Yar.B Complex


Attachments

  • Source Code (44409-9428-Source-Code.zip)
  • Comments

    Author: dotnet programmer07 Jan 2013 Member Level: Gold   Points : 0

    excellent keep good work going



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: