日期:2014-05-18  浏览次数:20407 次

不用模板,生成静态页,大家来踩!
菜鸟代码,欢迎高手践踏

static public void ConvertHtml(string urlFile)
{
  if(urlFile!="")
  {
StringWriter writer = new StringWriter();
System.Web.HttpContext.Current.Server.Execute(urlFile,writer);
if (writer!=null)
{
try 

using(StreamWriter sw=new StreamWriter(System.Web.HttpContext.Current.Server.MapPath(Convert.ToString(new Random().Next())+".html"),false,System.Text.Encoding.GetEncoding("GB2312"))) 
sw.WriteLine(writer.ToString()); 
sw.Flush(); 
sw.Close(); 

catch (Exception ex)

throw ex;
//System.Web.HttpContext.Current.Response.Write ("The file could not be wirte:"); 

}
  }
}

------解决方案--------------------
是个思路,直接写。麻烦在于,页面设计很麻烦的
------解决方案--------------------
public class CacheModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;

if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".aspx"))
{
string fileUrl = "~/cache/";
string fileName = GetFileName(context);
string filePath = context.Server.MapPath(fileUrl) + fileName;
if (File.Exists(filePath))
{
context.RewritePath(fileUrl + fileName, false);
}
}
}
public static string GetFileName(HttpContext context)
{
return context.Request.AppRelativeCurrentExecutionFilePath.ToLower()
.Replace(".aspx", "").Replace("~/", "")
+ context.Request.Url.Query.Replace("?", "__").Replace("&", "_") + ".html";

}
public void Dispose() { }
}
------解决方案--------------------
感觉还是用模板页面比较好。