How to freeze a dynamic aspx page into a static html page (on the server).

Take this scenario: On a website a visitor fills in a form after which the form is to be filed as a static html page. You might have several reasons for wanting to do that, one of them are search engines. Getting it done in asp.net took me more effort than I had originally expected. Let me share what I have found to be working.

The easy way to catch the html rendered by asp.net is to save a page in the client browser. To catch it on the server you have to hook into the (html) rendering process. There is no Page.OnRender event but the Page class does have a Render method. The method has a protected visibility, you cannot  invoke it form your code. The Render method is invoked when asp.net renders the page to the output, to hook in you override the method. The Render method has a parameter of type HtnlTextWriter, the default implementation of the overriden Render method is invoking Base.Render passing it the HtmlTextWriter object.

protected override void Render(HtmlTextWriter writer)
{
   // Default behavior
  
base.Render(writer);
}

This gives a place to hook in an own textwriter where asp,net can render the html into. The constructor of the HTMLtextwriter class has a protected visibility. Passing an own htmltextwriter requires specializing the frameworks's HTMLtextwriter class. The constructor of the base class requires a stringwriter. A wrapper class to house both custom HTMLtextwriter and stringwriter:

internal class MyHtmlFileCreator
{
   private StringWriter html;
   private MyHtmlTextWriter htmlWriter;

   // override the HtmlTextWriter to reach the constructor
   // the constructor in the base class is protected 
   class MyHtmlTextWriter : HtmlTextWriter
   {
      internal MyHtmlTextWriter(TextWriter tw): base(tw){}
   }

   // publish the HTMLwriter
   internal HtmlTextWriter RenderHere
   {
      get {return htmlWriter;}
   }

   // constructor initializes stringwriter and htmlwriter based on that
   // initialize Url 
   internal MyHtmlFileCreator()
   {
      html = new StringWriter();
      htmlWriter = new MyHtmlTextWriter(html);
      newUrl = Context.Request.Url.AbsolutePath.ToString();
      newUrl = newUrl.Replace(".aspx",".htm");
   }

   internal void WriteHTMLFile(string virtualFileName)
   {
      // Stringreader reads output rendered by asp.net
      // Stringwriter writes html output file
      StringReader sr = new StringReader(html.ToString());
      StringWriter sw = new StringWriter();

      // Read from input
      string htmlLine = sr.ReadLine();
      while (htmlLine != null)
      {
      // Filter out ASp.net specific tags
      if (! ((htmlLine.IndexOf("<form") > 0) ||
            (htmlLine.IndexOf("__VIEWSTATE") > 0) ||
            (htmlLine.IndexOf("</form>") > 0) ))
         {sw.WriteLine(htmlLine);}

      htmlLine = sr.ReadLine();
      }

      // Write contents stringwriter to html file
     
StreamWriter fs = new StreamWriter(virtualFileName);
      fs.Write(sw.ToString());
       fs.Close();
   }

}

A MyHtmlFileCreator object has a place asp.net can render to and it has a method to write the contents of the stringwriter (inside the htmlwriter) to file. I use the MyHtmlFileCreator in a page base class. The page hooks in the render event. When the freeze flag is set the html is rendered to a file and the application is redirected to the html file just created.

public class FreezablePage : System.Web.UI.Page
{
   internal class MyHtmlFileCreator{}

   // When Asp.Net renders the page the Page.Render method is invoked
   // Override the method to hook in

   protected override void Render(HtmlTextWriter writer)
   {
      if (freeze)
      {
         MyHtmlFileCreator htmlFile = new MyHtmlFileCreator();
         // Let Asp.net render the output, catch it in the file creator
         base.Render(htmlFile.RenderHere);
         // Write new html file
         htmlFile.WriteHTMLFile(Server.MapPath(NewUrl));
         // Redirect
         Response.Redirect(NewUrl, true);
      }
   else
   {
      // Default behavior
      base.Render(writer);
   }

   }

   // Flag render event
   protected void Freeze()
  {
      freeze = true;
   }

   protected void Freeze(string toUrl)
   {
      freeze = true;
      NewUrl = toUrl;
   }

   private bool freeze = false;

   private string newUrl;

   internal string NewUrl
   {
      get
      {
         return newUrl;
      }
      set
      {
          newUrl = value;
      }

   }

   }

}

Use this on your page like this :

public class _Default : Gekko.Web.UI.FreezablePage
{
    private void Button1_Click(object sender, System.EventArgs e)
   {
      Freeze(string.Format(@"Statisch/{0}.htm", TextBox1.Text));
   }
}

You will see that the resulting page has lost its form knocking out al buttons. There are no more postback possible. And the viewstate is also cut away. To process any file asp.net might produce will require more filtering. You can do quite flexible things treating the input as xml. Which requires some cutting and pasting, an html document is not always well-formed.

BTW Firefox was a lovely sidekick. The way it  displays the html source of a page..mmm:>

Peter

你可能感兴趣的:(dynamic)