问题
Anyone got an idea of how to render an aspx page inside of an HttpModule and stream it back to the browser?
回答1:
You can do something like this:
Type page_type = BuildManager.GetCompiledType ("~/page.aspx");
Page page = (Page) Activator.CreateInstance (page_type);
page.ProcessRequest (Context);
回答2:
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());
}
}
回答3:
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.
来源:https://stackoverflow.com/questions/1732184/render-a-page-inside-of-an-httpmodule