C#加载前生成静态网页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;

namespace test
{
public class PageCreateRenderPage : System.Web.UI.Page
{
public string strFileName = "default.html";

public string redirectURL = null;

public PageCreateRenderPage() { }

protected override void Render(HtmlTextWriter writer)
{
TextWriter tw = new StringWriter();
base.Render(new HtmlTextWriter(tw));
tw.Close();
string html = tw.ToString();
int urllen = Request.Url.AbsoluteUri.LastIndexOf('/');
string url = Request.Url.AbsoluteUri.Substring(urllen + 1);
strFileName = Server.MapPath(".") + "/" + url.Split('.')[0]+".html";
writeHtml(strFileName, html);
Response.Redirect(url.Split('.')[0]+".html");
}

public static void writeHtml(string filmname, string html)
{
System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");
string htmlfilename = filmname;
string str = html;
StreamWriter sw = null;
// 写文件
try
{

sw = new StreamWriter(htmlfilename, false, code);

sw.Write(str);

sw.Flush();

}

catch (Exception ex)
{

HttpContext.Current.Response.Write(ex.Message);

HttpContext.Current.Response.End();

}

finally
{

sw.Close();

}

}
}
}

原文地址:https://www.cnblogs.com/codeloves/p/3337443.html