[英]How to save current aspx page as html
請告訴我如何在按鈕點擊時將當前頁面保存為html頁面。 我的頁面僅包含我在頁面加載事件中填寫的標簽。
我正在使用下面的代碼,但它沒有保存(在HTML中)我加載頁面時看到的所有值(我認為它會在頁面上加載值之前進行轉換)。
private void saveCurrentAspxToHTML()
{
string HTMLfile = "http://localhost:4997/MEA5/AEPRINT.aspx?id=" +
Convert.ToString(frmae.AeEventid) +
"&eid=" +
Convert.ToString(frmae.AeEnquiryid);
WebRequest myRequest = WebRequest.Create(HTMLfile);
// Return the response.
WebResponse myResponse = myRequest.GetResponse();
// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipe the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(ReceiveStream, encode);
// Read 256 charcters at a time.
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
using (StreamWriter sw = new StreamWriter(Server.MapPath("~") + "\\MyPage.htm"))
{
while (count > 0)
{
// Dump the 256 characters on a string and display the string onto the console.
String str = new String(read, 0, count);
sw.Write(str);
count = readStream.Read(read, 0, 256);
}
}
// Close the response to free resources.
myResponse.Close();
}
請幫我!
我對實際代碼有點粗略。 但不久前我做了類似的事情。 我使用StringWriter將aspx的內容寫入html字符串。
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
divForm.RenderControl(w);
string s = sw.GetStringBuilder().ToString();
那么基本上你只需要將它寫入一個字符串文件並將其保存為HTML擴展。
System.IO.File.WriteAllText(@"C:\yoursite.htm", s);
我知道問題發布已經有6年了。 但是,我在這里得到了一個很好的參考: https : //weblog.west-wind.com/posts/2004/Jun/08/Capturing-Output-from-ASPNet-Pages
也許它會對其他讀者有用。
protected override void Render(HtmlTextWriter writer)
{
// *** Write the HTML into this string builder
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hWriter = new HtmlTextWriter(sw);
base.Render(hWriter);
// *** store to a string
string PageResult = sb.ToString(); //PageResult contains the HTML
// *** Write it back to the server
writer.Write(PageResult)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.