Skip to main content
Email Password   helpLost your password?

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:

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

STEP 2

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" %>
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        // If the requested file exists

        if (File.Exists(Request.PhysicalPath))
            { 
                // Do nothing here, just serve the file

            }
        // If the file does not exist then

        else if (!File.Exists(Request.PhysicalPath))
            {
            // Get the URL requested by the user

            string sRequestedURL = Request.Path.Replace(".aspx", "");
            
            // You can retrieve the ID of the content from database that is 

            // relevant to this requested URL (as per your business logic)

            int nId = 0;        
            ////// nId = GetContentIDByPath(sRequestedURL); \\\\\


           // The ShowContents.aspx page should show contents relevant to 

           // the ID that is passed here

            string sTargetURL = "~/ShowContents.aspx?ContentID=" + nId.ToString();

            // Owing to RewritePath, the user will see requested URL in the

        // address bar

            // The second argument should be false, to keep your references

        // to images, css files

            Context.RewritePath(sTargetURL, false);
        }
    }
//  That's all! :-) 

 

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralProblem with styles Pin
benny60
13:36 17 Oct '09  
GeneralRe: Problem with styles Pin
Manu Agrawal
14:25 12 Nov '09  
Questionimages and css file path Pin
deching
20:45 17 May '09  
AnswerRe: images and css file path Pin
Manu Agrawal
14:27 12 Nov '09  
GeneralAnother easy and efficient approach sample code Pin
samardeep
22:04 6 Apr '09  
GeneralFor Simplest Way of writing URL Rewriting Pin
DotNetGuts
11:31 24 Jul '08  
GeneralWhy Not Use UrlRewriter.Net? Pin
nickyt
7:04 19 Jun '08  
GeneralQuestion Pin
Ashley van Gerven
17:36 17 Jun '08  
GeneralDoesn't work! Pin
adalbas
6:46 12 Jun '08  
GeneralURL Rewriting Advice Pin
atwalb
7:41 11 Jun '08  
Newsimages and css not load on main page Pin
shaan
22:58 11 May '08  
Generalimages not working Pin
Steve456
17:05 18 Mar '08  
GeneralRe: images not working Pin
Manu Agrawal
11:47 15 Apr '08  
GeneralExcellent Pin
ultradream
5:20 5 Mar '08  
Generalwhy do i lose all my images and my css ? Pin
greekhand
11:13 19 Sep '07  
AnswerRe: why do i lose all my images and my css ? Pin
Manu Agrawal
11:51 19 Sep '07  
GeneralRe: why do i lose all my images and my css ? Pin
greekhand
12:42 19 Sep '07  
Generali tried some stuff , got another question ... Pin
greekhand
2:28 20 Sep '07  
GeneralRe: i tried some stuff , got another question ... Pin
Manu Agrawal
3:35 22 Sep '07  
GeneralRe: i tried some stuff , got another question ... Pin
greekhand
5:17 25 Sep '07  
GeneralDotNetNuke friendly urls Pin
Tommi G
16:03 16 May '07  
GeneralPOST back problems Pin
Andrei Rinea
11:55 8 May '07  
NewsValidators and WebResource.axd in general Pin
Andrei Rinea
6:12 3 May '07  
GeneralRe: Validators and WebResource.axd in general Pin
Manu Agrawal
6:57 3 May '07  
AnswerRe: Validators and WebResource.axd in general Pin
Andrei Rinea
7:09 21 May '07  


Last Updated 21 Apr 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009