首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

不用模板,生成静态页,大家来踩!该怎么解决

2012-01-29 
不用模板,生成静态页,大家来踩!菜鸟代码,欢迎高手践踏static public void ConvertHtml(string urlFile){if

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

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() { }
}
[解决办法]
感觉还是用模板页面比较好。

热点排行