Click here to Skip to main content
12,818,688 members (28,691 online)
Click here to Skip to main content
Add your own
alternative version

Stats

176.7K views
2.5K downloads
108 bookmarked
Posted 8 Apr 2007

URL Mapping / URL Rewriting / Search Engine Friendly URLs / Virtual URLs (ASP.NET)

, 21 Apr 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
URL Mapping / URL Rewriting / Search Engine Friendly URLs / Virtual URLs with postbacks in ASP.NET C# (Without hardcoding in Web.Config)

Introduction

(ASP.NET, C#, Visual Studio 2005)

Hi folks; Recently, I built a custom Content Management Software for a website. The client wanted the page contents to be served from database by a page ShowContents.aspx?ContentID=XX.
The main requirements were as follows:

  • Search engine friendly URLs (i.e. They would like "/finance/loan.aspx" instead of "ShowContents.aspx?ID=23").
  • Hardcoding URLs in Web.Config's URLMapping section was not an option, as there were no physical pages to map to.
  • Postback should still work on all these virtual URLs.
  • References to the images, css files, and themes should stay intact.

Solution

After a day's research, I came up with this really simple solution. I thought I should sum up the knowledge collected from various sources and make it public for everyone to benefit.
You can integrate following steps in to your existing ASP.NET project, or you can create a new one.

STEP 1

  • Create "ShowContents.aspx", that serves content based on the ContentID passed in the QueryString.
  • To enable postbacks to the raw URL in the Page_Load() event of ShowContents.aspx, insert following line Context.RewritePath(Path.GetFileName(Request.RawUrl)); // needs System.IO

STEP 2

  • Capture the URL requested by the user in the Application_BeginRequest() event of Global.asax.
  • Find the ID of the content from database that is relevant to this URL
  • ReWritePath to ShowContents.aspx?ContentID=XX. The user will see requested URL in the address bar with the desired content.
  • Any files that physically exist on the server will still get served as usual.

Using the code

// STEP 1 (ShowContents.aspx)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This page displays contents from a database for a ContentID. This file
// must contain the following line in it's Page_Load() event.
Context.RewritePath(Path.GetFileName(Request.RawUrl)); //needs System.IO

// STEP 2 (Global.asax)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Following code goes in your Global.asax file, in the root directory of
// the project. This file captures the incoming requests' URL, gets the 
// Content ID of the content related to the URL and redirects the user to
// ShowContents.aspx?ContentID=XX. While the user still sees the requested 
// URL in the address bar.

    <%@ Import Namespace="System.IO" %>

Hope this helps you save time on researching various sources. You might like to download the zip file containing a sample VS 2005 project.

Feedback is welcome and appreciated.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Manu Agrawal
Web Developer
United States United States
Manu Agrawal is a .NET developer living in the USA.
He has good professional web development experience (mainly in ASP.NET, C#). He enjoys developing high performance and secure websites.
He holds a master's degree in E-Commerce and Management from London, UK.

You may also be interested in...

Comments and Discussions

 
GeneralProblem with styles Pin
benny6017-Oct-09 13:36
memberbenny6017-Oct-09 13:36 
GeneralRe: Problem with styles Pin
Manu Agrawal12-Nov-09 14:25
memberManu Agrawal12-Nov-09 14:25 
Questionimages and css file path Pin
deching17-May-09 20:45
memberdeching17-May-09 20:45 
AnswerRe: images and css file path Pin
Manu Agrawal12-Nov-09 14:27
memberManu Agrawal12-Nov-09 14:27 
GeneralAnother easy and efficient approach sample code Pin
samardeep6-Apr-09 22:04
membersamardeep6-Apr-09 22:04 
GeneralMessage Automatically Removed Pin
24-Jul-08 11:31
memberDotNetGuts24-Jul-08 11:31 
QuestionWhy Not Use UrlRewriter.Net? Pin
nickyt19-Jun-08 7:04
membernickyt19-Jun-08 7:04 
GeneralQuestion Pin
Ashley van Gerven17-Jun-08 17:36
memberAshley van Gerven17-Jun-08 17:36 
GeneralDoesn't work! Pin
adalbas12-Jun-08 6:46
memberadalbas12-Jun-08 6:46 
GeneralURL Rewriting Advice Pin
atwalb11-Jun-08 7:41
memberatwalb11-Jun-08 7:41 
Newsimages and css not load on main page Pin
shaan11-May-08 22:58
membershaan11-May-08 22:58 
Generalimages not working Pin
Steve45618-Mar-08 17:05
memberSteve45618-Mar-08 17:05 
GeneralRe: images not working Pin
Manu Agrawal15-Apr-08 11:47
memberManu Agrawal15-Apr-08 11:47 
GeneralExcellent Pin
ultradream5-Mar-08 5:20
memberultradream5-Mar-08 5:20 
Questionwhy do i lose all my images and my css ? Pin
greekhand19-Sep-07 11:13
membergreekhand19-Sep-07 11:13 
AnswerRe: why do i lose all my images and my css ? Pin
Manu Agrawal19-Sep-07 11:51
memberManu Agrawal19-Sep-07 11:51 
GeneralRe: why do i lose all my images and my css ? Pin
greekhand19-Sep-07 12:42
membergreekhand19-Sep-07 12:42 
Generali tried some stuff , got another question ... Pin
greekhand20-Sep-07 2:28
membergreekhand20-Sep-07 2:28 
GeneralRe: i tried some stuff , got another question ... Pin
Manu Agrawal22-Sep-07 3:35
memberManu Agrawal22-Sep-07 3:35 
GeneralRe: i tried some stuff , got another question ... Pin
greekhand25-Sep-07 5:17
membergreekhand25-Sep-07 5:17 
GeneralDotNetNuke friendly urls Pin
Tommi G16-May-07 16:03
memberTommi G16-May-07 16:03 
GeneralPOST back problems Pin
Andrei Rinea8-May-07 11:55
memberAndrei Rinea8-May-07 11:55 
NewsValidators and WebResource.axd in general Pin
Andrei Rinea3-May-07 6:12
memberAndrei Rinea3-May-07 6:12 
GeneralRe: Validators and WebResource.axd in general Pin
Manu Agrawal3-May-07 6:57
memberManu Agrawal3-May-07 6:57 
AnswerRe: Validators and WebResource.axd in general Pin
Andrei Rinea21-May-07 7:09
memberAndrei Rinea21-May-07 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 21 Apr 2007
Article Copyright 2007 by Manu Agrawal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid