在ASP.NET中,page其实就是一个HttpHandler,它处理请求,然后返回一大堆Html。那么是不是可以实例化一个Page类然后获得其处理的结果呢?一个简单的推理就能说明这是不行的。因为如果我们实例化Page,这样new Page(),这是一个标准的.net代码,编译器无法知道相关的Page.aspx的内容。所以需要PagePaser帮忙,并且PagePaser也是ASP.NET内部编译过程的关键步骤。下面看看使用方法:
protected void Page_Load(object sender, Ev上海闵行企业网站制作entArgs e)
{
Response.Write(HttpUtility.HtmlEncode(DumpHtmlFrom("~/Default.aspx"上海徐汇企业网站制作"color: #000000;">)));
}
//virtualPath: allow queries
string DumpHtmlFrom(string virtualPath)
{
StringWriter writer = new上海网站建设n> 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 = n上海闵行企业网站设计与制作tp://www.93tj.com'>上海企业网站制作ew HttpContext(new HttpRequest(virtualPath, path, query), new HttpResponse(writer));
var handler = PageParser.GetCompiledPageInstance(virtualPath, MapPath(virtualPath), context);
handler.ProcessRequest(context);
return writer.ToString();
}
{
Response.Write(HttpUtility.HtmlEncode(DumpHtmlFrom("~/Default.aspx"上海徐汇企业网站制作"color: #000000;">)));
}
//virtualPath: allow queries
string DumpHtmlFrom(string virtualPath)
{
StringWriter writer = new上海网站建设n> 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 = n上海闵行企业网站设计与制作tp://www.93tj.com'>上海企业网站制作ew 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。