|
在ASP.NET中,page其實(shí)就是一個(gè)HttpHandler,它處理請(qǐng)求,然后返回一大堆Html。那么是不是可以實(shí)例化一個(gè)Page類然后獲得其處理的結(jié)果呢?一個(gè)簡單的推理就能說明這是不行的。因?yàn)槿绻覀儗?shí)例化Page,這樣new Page(),這是一個(gè)標(biāo)準(zhǔn)的.NET代碼,編譯器無法知道相關(guān)的Page.ASPx的內(nèi)容。所以需要PagePaser幫忙,并且PagePaser也是ASP.NET內(nèi)部編譯過程的關(guān)鍵步驟。下面看看使用方法:
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();
}
NET技術(shù):用PagePaser創(chuàng)建Page作為HttpHandler,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。