2010-07-17 12 views
7

Por favor dígame cómo guardar la página actual como una página html al hacer clic en el botón. Mi página contiene solo etiquetas que llené en el evento de carga de página.Cómo guardar la página aspx actual como html

Estoy utilizando el código siguiente para esto, pero no guarda (en HTML) todos los valores que veo cuando se carga mi página (creo que se convierte antes de que los valores se carguen en la página).

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

} 

Por favor ayuda!

Respuesta

3

Soy un poco superficial en el código real. Pero hace un tiempo hice algo similar. Usé un StringWriter para escribir el contenido de la cadena aspx a html.

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

Básicamente, solo tiene que escribir eso en un archivo de cadena y guardarlo como una extensión HTML.

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

Puede por favor dar una demostración o documentación? –

2

Sé que ya han pasado 6 años desde que se publicó la pregunta. Pero, tengo una buena referencia aquí: https://weblog.west-wind.com/posts/2004/Jun/08/Capturing-Output-from-ASPNet-Pages

Tal vez sea útil para el otro lector.

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) 
} 
Cuestiones relacionadas