Home >> ASP.NET >> URL Re-write using Global.asax in ASP.NET

URL Re-write using Global.asax in ASP.NET


Lets come back to ASP.NET series again, We have few more articles on ASP.NET before. This article is about making your website more SEO friendly. In the same topic, we have published a step by step tutorial on how to get dynamic meta tags and description from DataBase.

URL Re-write using Global.asax

Lets start with simple steps, We will show you example of our CMS project that is under construction. However, we will try to cover the basic steps from beginning to final to get job done with screenshots.


As you add new item in your project in visual studio, you see a file type called Global Application Class.

Adding Global.asax
Adding Global.asax

Now open the newly added Global.asax file and import a namespace called Web.Routing. Lets have look at code.

Now create a function with any convenient names with below details. I have created function with RegisterRoutes

Let’s discuss the code above:

DefaultDownloads is just the name.
Song/{SongID}/{SongURL}.aspx Song is just text and {SongName} & {SongURL} are two variables which we will replace with value in Browser’s Address bar and use for searching in database.
“~/SongDetails.aspx” is page which will do the query display data in users page according to PageRoutes.

In the CMS we are developing, we have currently three modules ready, so we have three RouteCollections named one for website, one for Downloads and one for news.
If you want to see live working demo of above code, we have Shared Hosting that is fully customized which is hosting Download Center. You can click on any of song there and you can see dynamic URL which seems SEO friendly.

You can add as much as you want but work is not done yet. We still have to do something in Global.asax file.

We have to set the above function execute on Application start event. Global.asax is designed to control the global settings like what to do when Application Starts or Ends or gets error, Session start & Session Ends.

Now you are all set on part of Global.asax

Lets move to the file which will handle the request.

Query on ASPX page

But remember, to be so we have the Address set to correct location. Lets have look at our logic what we have used to re-direct here.

Have a look at above code, the code is in similar format as in Global.asax file, want reminded, here is the code.

GetDataFromURL() : This function will perform the all query from database and browser address bar.

Page.RouteData.Values[“SongID”].ToString(); : If you remember, we have put {SongName} in global.asax page earlier. This the the same. We have requested the value of SongName from browser. .

GetData(songID): This is the same ID we will use to query and GetData is a function that will load data based on SongID.

Lets have a look at working demo.

URL Re-write using Global.asax

URL Re-write using Global.asax

About John Bhatt

Aka, Prithvi Raj Bhatt, A Proud Nepali, Blogger, Software Developer, ASP.NET, Web Designer & Developer, Faculty and a Troubleshooter blog @PRsBlog

Check Also

ASP.NET 5 Launched

Hi, This time we are glad to inform you, Microsoft has just announced ASP.NET 5 …

Leave a Reply

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