通过aspx页面生成静态页面,在*.cs文件重写page的Render方法,接收页面通过请求返回的hmtl代码,然后再另存为*.html模板

代码如下:

方法一:把本页面生成html页面

protected override void Render(HtmlTextWriter writer)
    {
        System.IO.StringWriter html = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);
        base.Render(tw);
        System.IO.StreamWriter sw;
        sw = new System.IO.StreamWriter("D:\\a.htm", false, System.Text.Encoding.Default);
        sw.Write(html.ToString());
        sw.Close();
        tw.Close();
        Response.Write(html.ToString());
    }

 

方法二:请求其它页面,再生成静态页面(但不能使本页面生成静态)

protected void btn_Submit_Click(object sender, EventArgs e)
    {
        System.IO.StringWriter swHtml = new System.IO.StringWriter();
        Server.Execute("Default2.aspx", swHtml);
        string contentStr = swHtml.ToString();
        string filePath = "d:\\";
        if (!System.IO.Directory.Exists(filePath))
        {
            System.IO.Directory.CreateDirectory(filePath);
        }

        filePath += "1.html";
        System.IO.StreamWriter sWrite = new System.IO.StreamWriter(filePath, true, System.Text.Encoding.Default);
        sWrite.Write(contentStr);
        sWrite.Flush();
        sWrite.Close();
    }

 

相关文章:

  • 2021-09-01
  • 2022-12-23
  • 2022-03-05
  • 2022-02-14
  • 2022-01-26
  • 2022-12-23
猜你喜欢
  • 2021-12-28
  • 2022-12-23
  • 2021-11-21
  • 2021-07-05
  • 2022-03-06
相关资源
相似解决方案