Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Serving up Virtual Web pages from a Database

Posted on 2009-02-23
Medium Priority
Last Modified: 2012-05-06
I have written a Content Management System for my clients. The pages are stored in a SQL Server Database. It is a simple enough task to create an empty physical file for each of the Pages in the database with a little bit of code that retrieves the content.

This means of course that if the client adds a new page, or moves one in the hierarchy or deletes one then I need to do the same to the physical file. Which is OK but I have dreams of a better system.

I don't want to create a physical file for each of the pages in the database, I want the visitor to hit a link to a page...


...and have the my_virtual_page served up from the database. I wonder how other Developers acheive this, for instance...


...no physical file, all from a Content Management system.

Any ideas would be greatfully received!
Question by:Tech4biz
  • 5
  • 2
LVL 31

Expert Comment

by:Wayne Barron
ID: 23716918
Here you go. (True Dynamic)
source code

Should be easy to understand, if not, just let me know.

(CLick on the # links to advance to the next page)
LVL 28

Expert Comment

ID: 23719764
As carrzkiss says. You have a single ASP file, and based on the value of an id in the querystring, you can refer to a record in the database.

Another interesting option (which will give you the possibility of having truelly virtual pages, is to edit the 404-error page. In the IIS panel you can change the standard 404-error page to an ASP which you have created yourself.

This new 404-error page should then determine the path of the virtual page and link it to content in the database. So example you'd create an entry "very_interesting_page.asp" in your database (but not on the file system). The visitor goes to http://www.yourdomain.com/very_interesting_page.asp. Because that page does not physically exists, IIS redirects the visitor to the 404-error-page, which you have cleverly set up that the user gets to see the right content anyway.

Author Comment

ID: 23719872

That is exactly what I am doing (but without the .asp extension visible)!

I started another Thread looking at my problem from a deeper postion before posting this one and with Carrzkiis help I have now got it to a place were I have made it understandable. The thread is here...


...but in a nutshell, I am doind exactly what you suggest - set up a 404 error page in IIS that interrogates the URL and servers up the corresponding page from the DB.

This works like a treat...


...but my problems start there! What happens when one of my vurtual pages (for instance a contact page) is submitted (POSTed) back to itself? As there has been another step in the process (via the IIS error handler) the querystring doesn't make it (or does it) to the final page after POSTing and redirecting.

This is the solution I am looking for is how to use this method and still be able to pick up the querystring?

Thanks guys...
Kevin Russell
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 23719889
Oops! The link in the last post was incorrect - but it did make me realise I need to pretty up my 404 page!


Author Comment

ID: 23733744

I have done some practical testing and the QueryString is in fact passed back to the handler.asp file so the post back works great...


...enter something in the box and click the submit button. This leaves me with another question!!!

As the Page contents are being pulled from a database it is not possible to insert ASP code into the HTML stream. I need some way of inserting code.

For instance, in test example I would show a contact form, then it would submit back to itself. From here I would check the form contents and either show the form again to check the contents or just show a label "thanks for submiting".

Any ideas?
...or is there a better way?
LVL 28

Expert Comment

ID: 23734025
ExecuteGlobal() is the function you might want to use. That handles multiple-line code.

Accepted Solution

Tech4biz earned 0 total points
ID: 23734505
I have a solution. I will post it here in case anyone else is interested. Bear in mind that this is my own solution to a particular problem. Any suggestions as to how to improve it will be gratefully received.

To create Search Engine Friendly URLS with Dynamic Pages stored in a Database using Classic ASP

Add the pages to a Database - store the header and footer in a separate table for simplicity
Create a single ASP handler page that interrogates the 404 URL (code below)
In IIS or your Hosting account, set up an Error page for 404 errors and point it to your ASP handler page
When the page is called (via a 404 error) retrieve the file part of the URL, select the appropriate page data from the Database and write it.
With the correct hosting, the original URL is retained.

To make the pages dynamic and to be able to integrate Dynamic ASP results into the Page Contents
Add a marker in the page data that is stored in the Database...


By checking the Page Contents for the EXECUTE Marker, it is possible to replace it with a pre-written function in the ASP Handler file (code below).

...some functions used in the code below are stored in an include file. The Header and Footer are stored once in the Database separately but retrieved with every request for page data.
'Example code for ASP Handler file
'Retrieve the Page from the Database (replace Meta Data with data stored in the Database)
Call GetPageFromDatabase
Function GetPageFromDatabase
    Dim objRS, aParameters(1), cHeader
    aParameters(1) = GetPage 'See function below
    Set objRS = GetRecordset("GetPageByName", aParameters) 'Sends a Parameter to a Stored Procedure to retrieve a Recordset
    If Not objRS.EOF Then
        cHeader = objRS("_Header")
        cHeader = Replace(cHeader, "META_TITLE", objRS("Meta Title"))
        cHeader = Replace(cHeader, "META_DESCRIPTION", objRS("Meta Description"))
        cHeader = Replace(cHeader, "META_KEYWORDS", objRS("Meta Keywords"))
        Response.Write cHeader
        Call WriteContent(objRS("Content"))
        Response.Write objRS("Footer")
        Response.Write "The Page """ & aParameters(1) & """ was not found in the CMS Database<br>"
    End If
    Set objRS = Nothing
End Function
'Get the requested page name from the URL page passed by IIS Error handler
Function GetPage
    Dim aBaseURLs(2), cTempPage, i
    aBaseURLs(1) = "404;http://www.mydomain:80/"
    aBaseURLs(2) = "404;http://mydomain.com:80/"
    On Error Resume Next
        cTempPage = Request.QueryString
        For i = 1 To Ubound(aBaseURLs)
            cTempPage = Replace(cTempPage, aBaseURLs(i), "")
        If Right(cTempPage, 1) = "/" Then cTempPage = Left(cTempPage, Len(cTempPage)-1)
        If cTempPage = "" Then cTempPage = "home"
        GetPage = cTempPage
    On Error Goto 0
End Function
'Check for Execute Markers and call the relevant Function
Sub WriteContent(cContent)
    Dim aContent, iThisIndex
    aContent = Split(cContent, "///")
    For iThisIndex = 0 to Ubound(aContent)
        If aContent(iThisIndex) = "EXECUTE" Then
            If iThisIndex < Ubound(aContent) Then
                Response.Write Execute(aContent(iThisIndex + 1)) 'This line executes the Function or Sub indicated in the page content
                iThisIndex = iThisIndex + 1 'Step over the Function name
            End If
            Response.Write aContent(iThisIndex)
        End If
End Sub
'This is the pre-written function added to the Page Content
Function MyFunction
    If Request.Form("name") > "" Then
        Response.Write "Thank you for your interest " & Request("name")
        Response.Write "<form name='test' action='/test' method='post'/>" & _
                   "name: <input name='name' value='" & Request("textbox") & "' type='text'/>" & _
                   "<input name='submit' value='submit' type='submit'>" & _
    End If
End Function

Open in new window


Author Comment

ID: 23734577

Thanks for the recommendation. I think ExecuteGlobal() would achieve teh same results.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question