ASP.NET 伪静态页面生成

必须重载OnInit和Render实现的静态页面生成
但读写个类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// change 的摘要说明
/// </summary>
public class change : System.Web.UI.Page
{
    protected string FileName;
protected override void OnInit(EventArgs e)
    {
        int count = Request.Url.AbsolutePath.Length;
        FileName = Request.Url.AbsolutePath.Remove(count - 4, 4) + "htm";//把要转换的页面后缀aspx去掉
        if (File.Exists(Server.MapPath(FileName)))
        {
            File.Delete(Server.MapPath(FileName));
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        StringWriter html = new StringWriter();
        HtmlTextWriter tw = new HtmlTextWriter(html);
        base.Render(tw);
        StreamWriter sw = new StreamWriter(Server.MapPath(this.FileName), false, System.Text.Encoding.UTF8);
        sw.Write(html.ToString());
        sw.Flush();
        sw.Close();
        tw.Close();
        Response.Write(html.ToString());
    }
}
要生成静态页的页面只需继承这个类(change)就可以在你的网站路径下面生成了一个静态页面名字和动态页面相同就是是静态页

你可能感兴趣的:(html,String,System,Class,asp.net)