日期:2014-05-17 浏览次数:20517 次
using system.net; using system.io; First:在服务器上指定aspx网页,生成html静态页 public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { StreamWriter sw = new StreamWriter(Server.MapPath("静态页1.htm"), false, System.Text.Encoding.GetEncoding("gb2312")); Server.Execute("Default3.aspx", sw); sw.Close(); } } } Second:在服务器上执行aspx网页时在page_render事件里将本页面生成html静态页 public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected override void Render(HtmlTextWriter writer) { StringWriter html = new StringWriter(); System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html); base.Render(tw); System.IO.StreamWriter sw; sw = new System.IO.StreamWriter(Server.MapPath("静态页2.htm"), false, System.Text.Encoding.Default); sw.Write(html.ToString()); sw.Close(); tw.Close(); Response.Write(html.ToString()); } } Third:从指定连接获取源代码生成html静态页 public partial class Default4 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string pageurl = "http://www.baidu.com"; WebRequest request = WebRequest.Create(pageurl); WebResponse response = request.GetResponse(); Stream resstream = response.GetResponseStream(); StreamReader sr = new StreamReader(resstream, System.Text.Encoding.Default); string contenthtml = sr.ReadToEnd(); resstream.Close(); sr.Close(); //写入文件 System.IO.StreamWriter sw; sw = new System.IO.StreamWriter(Server.MapPath("静态页生成方法3.htm"), false, System.Text.Encoding.Default); sw.Write(contenthtml); sw.Close(); } } }
------解决方案--------------------
模板替换的方法
http://www.chenjiliang.com/Article/View.aspx?ArticleID=3657&TypeID=5
------解决方案--------------------
上面的没选效果,重发一次哈.
模板页:NewsTemp.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>NewsTemp</title> </head> <body style="text-align: center"> <table style="width: 659px; height: 283px"> <tr> <td colspan="4"> $hader$</td> </tr> <tr> <td colspan="1" style="width: 158px"> $left$</td> <td colspan="3"> $content$</td> </tr> <tr> <td colspan="4"> $feet$</td> </tr> &