2010-07-17 18 views
7

Per favore dimmi come salvare la pagina corrente come una pagina HTML sul pulsante clic. La mia pagina contiene solo le etichette che riempio sull'evento di caricamento della pagina.Come salvare la pagina attuale di aspx come HTML

Sto usando il codice qui sotto per questo, ma non sta salvando (in HTML) tutti i valori che vedo quando viene caricata la mia pagina (penso che converta prima che i valori vengano caricati sulla pagina).

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

} 

Per favore aiutatemi!

risposta

3

Sono un po 'approssimativo sul codice effettivo. Ma qualche tempo fa ho fatto qualcosa di simile. Ho usato un StringWriter per scrivere il contenuto dell'asx su una stringa html.

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

Quindi, in pratica, è sufficiente scriverlo in un file di stringa e salvarlo come estensione HTML.

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

puoi fornire qualche demo o documentazione? –

2

So che sono già 6 anni da quando la domanda è stata postata. Ma ho qui una buona referenza: https://weblog.west-wind.com/posts/2004/Jun/08/Capturing-Output-from-ASPNet-Pages

Forse sarà utile per l'altro lettore.

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) 
} 
Problemi correlati