Serving up Virtual Web pages from a Database

Posted on 2009-02-23
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... physical file, all from a Content Management system.

Any ideas would be greatfully received!
Question by:Tech4biz
    LVL 30

    Expert Comment

    by:Wayne Barron
    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

    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 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


    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

    Author Comment

    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


    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

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

    Accepted Solution

    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;"
        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


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

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    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…
    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now