Search code examples
c#httpmodule

Dynamic Pages From a Database in C#


Forgive me if this has already been asked somewhere, but I cannot figure out the best way to accomplish this task. I want to be able to create a rendering system that will allow me to render out content from thousands of different .aspx pages without having to create thousands of .aspx pages. That being said, I still want to be able to render out the appropriate .aspx page if it exists in my code.

For example, when a request is made to the site, I want to check and see if that URL is in the database, if it is, then I want to render the content appropriately. However, if it doesn't, then I want it to continue on to rendering the real .aspx page.

In trying to use an HTTPModule, I cannot get the page that exists in the database to write out the appropriate content. Here's my code.

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication application = sender as HttpApplication;
        Uri url = application.Context.Request.Url;

        //Checks to see if the page exists in the database
        PageInformation page = PageMethods.GetPageFromUrl(url.AbsolutePath);

        if (page != null)
        {
            string renderedPage = Renderer.RenderPage(page);
            application.Context.Response.Write(renderedPage);
        }
    }

However, when trying to use an HTTPHandler, I can't get the real .aspx pages to render appropriately because the *.aspx verb is being dealt with by the handler.

If anyone has any better ideas on how to completely re-design this, I'm completely open to that as well. Thanks.


Solution

  • This will do the trick:

    Type page_type = BuildManager.GetCompiledType ("~/page.aspx");
    Page page = (Page) Activator.CreateInstance (page_type);
    page.ProcessRequest (Context);