2010-07-17 35 views
7

Vui lòng cho tôi biết cách lưu trang hiện tại dưới dạng trang html khi nhấp vào nút. Trang của tôi chỉ chứa các nhãn mà tôi điền vào sự kiện tải trang.Cách lưu trang aspx hiện tại dưới dạng html

Tôi đang sử dụng mã bên dưới cho điều này, nhưng nó không lưu (trong HTML) tất cả các giá trị tôi thấy khi trang của tôi được tải (tôi nghĩ nó chuyển đổi trước khi các giá trị được tải trên trang).

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(); 

} 

Hãy giúp tôi!

Trả lời

3

Tôi hơi sơ sài một chút về mã thực. Nhưng một thời gian trước tôi đã làm một cái gì đó tương tự. Tôi đã sử dụng một StringWriter để viết nội dung của aspx vào chuỗi html.

StringWriter sw = new StringWriter(); 
    HtmlTextWriter w = new HtmlTextWriter(sw); 
    divForm.RenderControl(w); 
    string s = sw.GetStringBuilder().ToString(); 

Sau đó, về cơ bản bạn chỉ cần viết tệp đó vào tệp chuỗi và lưu dưới dạng tiện ích mở rộng HTML.

System.IO.File.WriteAllText(@"C:\yoursite.htm", s); 
+0

bạn có thể vui lòng cung cấp một số bản demo hoặc tài liệu không? –

2

Tôi biết đã 6 năm kể từ khi câu hỏi được đăng. Nhưng, tôi có một tài liệu tham khảo tốt ở đây: https://weblog.west-wind.com/posts/2004/Jun/08/Capturing-Output-from-ASPNet-Pages

Có lẽ nó sẽ hữu ích cho người đọc khác.

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) 
} 
Các vấn đề liên quan