Display ASPX page at runtime from database

Assuming the following code:

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 the CreatePage method to create an instance of a page based on the simple contents of an ASPX string?

It should be noted that the ASPX line itself may contain a link to an existing MasterPage on disk.

I understand that there must be tremendous performance with this problem, but at this point I just want to know how I can do this. Obviously, I have to cache the result.

Thanks.

+7
source share
2 answers

The path you are trying to go down essentially loads the .aspx files from a different storage mechanism than the web server file system. You started to implement some of this, but you donโ€™t really need a custom HttpHandler to do this - ASP.NET has an existing mechanism for specifying other sources of actual ASPX markup.

It is called VirtualPathProvider , and it allows you to change the default functionality for downloading files from a disk, say, downloading them from SQL Server or somewhere else makes sense. Then you can take advantage of all the built-in compilation and caching that ASP.NET uses on its own.

The core of functionality is included in the GetFile and VirtualFile Open () methods :

 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); } } 

Please note: today, using a custom VirtualPathProvider requires full trust. However, ASP.NET 4.0 will be available soon, and it will support VPP under medium trust.

+9
source share

One way to do this is to subclass VirtualPathProvider and set it as HostingEnvironment.VirtualPathProvider by calling HostingEnvironment.RegisterVirtualPathProvider . You will have to override several methods. The most important is GetFile (). The build system takes care of caching.

+1
source share

All Articles