views:

175

answers:

3

Anyone got an idea of how to render an aspx page inside of an HttpModule and stream it back to the browser?

+2  A: 

You can do something like this:

Type page_type = BuildManager.GetCompiledType ("~/page.aspx");
Page page = (Page) Activator.CreateInstance (page_type);
page.ProcessRequest (Context);
Gonzalo
Perfect! Thanks.
David Lively
+1  A: 
public void ProcessRequest(HttpContext context)
{
    using (var writer = new StringWriter())
    {
        context.Server.Execute("default.aspx", writer);
        context.Response.ContentType = "text/html";
        context.Response.Write(writer.GetStringBuilder().ToString());
    }
}
Darin Dimitrov
+1 This does, in fact, work, but I'd rather avoid server.execute().
David Lively
A: 

The best way is probably to use URL rewriting to redirect the standard Handler processing step to the page you want to render. Something like:

context.RewritePath("yourpage.aspx", false);

You could run that from the BeginRequest event handler.

RickNZ