用PagePaser创建Page作为HttpHandler
时间:2011-01-06 来源:dragonpig
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(HttpUtility.HtmlEncode(DumpHtmlFrom("~/Default.aspx")));
}
//virtualPath: allow queries
string DumpHtmlFrom(string virtualPath)
{
StringWriter writer = new StringWriter();
var path = Request.Url.Scheme + "://" + Request.Url.Authority + VirtualPathUtility.ToAbsolute(virtualPath);
var parts = virtualPath.Split('?');
string query = string.Empty;
if (parts.Length > 1)
query = parts[1];
virtualPath = parts[0];
HttpContext context = new HttpContext(new HttpRequest(virtualPath, path, query), new HttpResponse(writer));
var handler = PageParser.GetCompiledPageInstance(virtualPath, MapPath(virtualPath), context);
handler.ProcessRequest(context);
return writer.ToString();
}
其中关键的步骤PageParser.GetCompiledPageInstance实例化了一个HttpHandler,并返回所有的Html。
相关阅读 更多 +