Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render ASPX page at runtime from database

Assuming the code below:

public class DynamicAspxHandler : IHttpHandler {
    bool IHttpHandler.IsReusable { get { return false; } }

    void IHttpHandler.ProcessRequest(HttpContext httpContext) {
        string aspxContent = PlainASPXContent();
        Page page = CreatePage(httpContext, aspxContent);
        page.ProcessRequest(httpContext);
    }

    Page CreatePage(HttpContext context, string aspxContent) {
        // How to implement this?
    }
}

how can I implement CreatePage method to instantiate a page based on the plain string content of ASPX?

The note is that the ASPX string itself can containt reference to already existing MasterPage on the disk.

I realise there must be huge performance issue with this but at this stage I just want to know how I can do that. Obviously I will have to cache the result.

Thanks.

like image 322
Dmytrii Nagirniak Avatar asked Nov 23 '09 05:11

Dmytrii Nagirniak


1 Answers

The path you're trying to go down is essentially loading ASPX files from some other storage mechanism than the web server file system. You've started to implement part of that, but you actually don't even need a custom HttpHandler to do this - ASP.NET has an existing mechanism for specifying other sources of the actual ASPX markup.

It's called a VirtualPathProvider, and it lets you swap out the default functionality for loading the files from disk with, say, loading them from SQL Server or wherever else makes sense. Then you can take advantage of all the built-in compiling and caching that ASP.NET uses on its own.

The core of the functionality comes in the GetFile method and the VirtualFile's Open():

public override VirtualFile GetFile(string virtualPath)
{
    //lookup ASPX markup
    return new MyVirtualFile(aspxMarkup);
}

//...

public class MyVirtualFile : VirtualFile
{
    private string markup;

    public MyVirtualFile(string markup)
    {
        this.markup = markup;
    }

    public override Stream Open()
    {
        return new StringReader(this.markup);
    }
}

Note that today, using a custom VirtualPathProvider does require full trust. However, soon ASP.NET 4.0 will be available and it supports VPPs under medium trust.

like image 182
Rex M Avatar answered Sep 27 '22 22:09

Rex M