aspx页面生成静态页面

通过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();
    }

 

你可能感兴趣的:(asp)