本菜鸟刚接触到asp.net生成静态页面,现得到一个方法,具体如下:
2、用模板替换: template.htm //模板文件
<html>
<head>
<title>$title$</title>
</head>
<body>
$body$
</body>
</html>
.cs代码文件
string title = "生成的网页标题";
string body = "生成的网页内容";
string filename = Server.MapPath("~/") + "frame_a/index.htm";
System.IO.StreamReader srm = new System.IO.StreamReader(filename,System.Text.Encoding.Default);
string mb = srm.ReadToEnd();
srm.Close();
string tempfile = Server.MapPath("~/") + "index.htm";
StreamWriter sr = new System.IO.StreamWriter(tempfile, false, System.Text.Encoding.Default);
mb = mb.Replace("$title$", title);
mb = mb.Replace("$body$", body);
sr.Write(mb);
sr.Close();请教:如果第二种方法显示新闻页面,而新闻页面的aspx页面中含有自定义控件ascx,如何生成静态页呢?template模板应该怎么建立呢?如能告知,感激不尽
2、用模板替换: template.htm //模板文件
<html>
<head>
<title>$title$</title>
</head>
<body>
$body$
</body>
</html>
.cs代码文件
string title = "生成的网页标题";
string body = "生成的网页内容";
string filename = Server.MapPath("~/") + "frame_a/index.htm";
System.IO.StreamReader srm = new System.IO.StreamReader(filename,System.Text.Encoding.Default);
string mb = srm.ReadToEnd();
srm.Close();
string tempfile = Server.MapPath("~/") + "index.htm";
StreamWriter sr = new System.IO.StreamWriter(tempfile, false, System.Text.Encoding.Default);
mb = mb.Replace("$title$", title);
mb = mb.Replace("$body$", body);
sr.Write(mb);
sr.Close();请教:如果第二种方法显示新闻页面,而新闻页面的aspx页面中含有自定义控件ascx,如何生成静态页呢?template模板应该怎么建立呢?如能告知,感激不尽
{
bool Flage = false;
StreamReader ReaderFile = null;
StreamWriter WrirteFile = null;
//修改mode.htm到inc目录下
strModeFilePath = "../inc/" + strModeFilePath;
string FilePath = HttpContext.Current.Server.MapPath(strModeFilePath);
Encoding Code = Encoding.GetEncoding("gb2312");
string strFile = string.Empty;
try
{
ReaderFile = new StreamReader(FilePath, Code);
strFile = ReaderFile.ReadToEnd(); }
catch (Exception ex)
{
throw ex;
}
finally
{
ReaderFile.Close();
}
try
{
int intLengTh = strNewsHtml.Length;
for (int i = 0; i < intLengTh; i++)
{
strFile = strFile.Replace(strOldHtml[i], strNewsHtml[i]);
}
WrirteFile = new StreamWriter(HttpContext.Current.Server.MapPath(strPageFilePath), false, Code);
WrirteFile.Write(strFile);
Flage = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{ WrirteFile.Flush();
WrirteFile.Close();
}
return Flage;
}
public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)
{
bool Flage = false;
StreamReader ReaderFile = null;
StreamWriter WrirteFile = null;
string FilePath = HttpContext.Current.Server.MapPath(strHtml);
Encoding Code = Encoding.GetEncoding("gb2312");
string strFile = string.Empty;
try
{
ReaderFile = new StreamReader(FilePath, Code);
strFile = ReaderFile.ReadToEnd(); }
catch (Exception ex)
{
throw ex;
}
finally
{
ReaderFile.Close();
}
try
{
int intLengTh = strNewsHtml.Length;
for (int i = 0; i < intLengTh; i++)
{
int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;
int intEnd = strFile.IndexOf(strEndHtml[i]);
string strOldHtml = strFile.Substring(intStart, intEnd - intStart);
strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);
}
WrirteFile = new StreamWriter(FilePath, false, Code);
WrirteFile.Write(strFile);
Flage = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{ WrirteFile.Flush();
WrirteFile.Close();
}
return Flage;
}生成静态页面的5种方案
//将aspx转变成静态HTML
wc.DownloadFile(url, Server.MapPath("静态页面")); 至于aspx页面的数据当然动态生成到你的模板中,这个方法很实用
1、用伪静态生成链接
2、在伪静态指向的文件里面生成静态页面
protected void Page_Load(object sender, EventArgs e)
{
Create();
}
protected void Create()
{
string path = Server.MapPath("../news/show" + Request.QueryString["aid"] + ".html");//生成页面的路径
if (!System.IO.File.Exists(path))
{
//开始生成
System.IO.StringWriter writer = new System.IO.StringWriter();
Server.Execute("show.aspx?aid=" + Request.QueryString["aid"]+"&classid="+Request.QueryString["classid"], writer, true);
Response.Write(writer.ToString());
using (System.IO.StreamWriter sw = System.IO.File.CreateText(path))
{
sw.Write(writer.ToString());
}
writer.Close();
//生成结束
}
else
{
Response.Redirect(path);
//定向到页面
} }
3、配置iis,是实际的静态页不会受到伪静态的影响(如不设置此步骤,还是会访问伪静态的页面)这样就可以了,用户点击页面的时候生成对应的静态页面
这样只是完成了静态页面 ,还不是静态化。静态页面中的动态部分可以通过ajax获取。
forearch(DataRow dr in dt.Rows)
{
WebClient wc=new WebClient();
wc.Encoding=System.Text.Encoding.UTF8;
string path=Request.Url.ToString().Replace("当前的页面","要获取下载的页面")+"?id="+dr["id"];
string content wc.DownloadString("Show.aspx?id="+dr["id"]);
File.WriteAllText(Server.MapPath("~/HTML/"+dr["id"]+".html"),content,System.Text.Encoding.UTF8);
}
有没有比这个更少占用服务器资源的方法呢,问题上的那种方法很不错,但是模板貌似没有办法和服务器控件一起用,只能放弃,有么有再好点的方法呢?
建议LZ找个现成的代码 你看看。。
http://www.51aspx.com/S/%E9%9D%99%E6%80%81%E9%A1%B5.html