日期:2014-05-17  浏览次数:20384 次

怎么做到网站全静态化最好是有个小实例
如题,
说一下思路也行 谢谢!

------解决方案--------------------
网站全静态化使用模板。
public bool WriteFile(string strText)
{
string path = HttpContext.Current.Server.MapPath("news/");
Encoding code = Encoding.GetEncoding("gb2312");
string temp = HttpContext.Current.Server.MapPath("text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd();
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=path + DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
str = str.Replace("title",strText);
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();
}
return true;
}
}
也可使用伪静态
http://topic.csdn.net/u/20090216/23/200596a5-24cc-4f40-8f24-f29b5e786c4c.html
------解决方案--------------------

/// <summary>
/// 转换成静态HTML
/// </summary>
/// <param name="path">动态页面路径 如Aritcle.aspx?id=1</param>
/// <param name="outpath">生成的HTML文件路径</param>
public void transHtml(string path, string outpath)
{
Page page = new Page();
StringWriter writer = new StringWriter();
page.Server.Execute(path, writer);
FileStream fs;
if (File.Exists(page.Server.MapPath("") + "\\" + outpath))
{
File.Delete(page.Server.MapPath("") + "\\" + outpath);
fs = File.Create(page.Server.MapPath("") + "\\" + outpath);
}
else
{
fs = File.Create(page.Server.MapPath("") + "\\" + outpath);
}
byte[] bt = Encoding.Default.GetBytes(writer.ToString());
fs.Write(bt, 0, bt.Length);
fs.Close();
}