Question

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.

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top