views:

483

answers:

1

What is the proper to make an IHttpHandler to have an existing .aspx page process the request? I would like to be able to compile the .aspx file into an IHttpHandler and then have it process the request. There is the PageParser.GetCompiledPageInstance method, however in the documentation it states its not for direct use from code. I know I can have apsx files be automatically directed to, or perform a RewritePath, however I would like to have the object reference to the handler.

+1  A: 

Here's one quick-n'-dirty way of doing it:

var virtualPath = "~/foo/bar.aspx"
var output = HttpContext.Current.Response.Output;

// Get the compiled page type (i.e. foo_bar_aspx)
Type controlType = BuildManager.GetCompiledType(virtualPath);

// "new()" it up
var pageInstance = Activator.CreateInstance(controlType);

// Execute it
HttpContext.Current.Server.Execute(pageInstance, output, true);
chadmyers