如果你要将a.aspx生成a.html 可以在b.aspx中执行下面这句代码就可以,运行后,在a.aspx同目录下就生成了a.htmlServer.Execute("a.aspx", new StreamWriter(Server.MapPath("a.html")));
这个,有些事情得你自己安排,譬如说我们按新闻在表中的id来命名 html文件名 Server.Execute("a.aspx?id="+newid, new StreamWriter(Server.MapPath(newid+".html")));或者按当前时间来命名 string nowtime=.... Server.Execute("a.aspx, new StreamWriter(Server.MapPath(nowtime+".html"))); 那么此时a.aspx如何显示最新一条记录呢. 在a.aspx的SQL命令中用 select top 1 * from table order by ID DESC
可以在b.aspx中执行下面这句代码就可以,运行后,在a.aspx同目录下就生成了a.htmlServer.Execute("a.aspx", new StreamWriter(Server.MapPath("a.html")));
这个,有些事情得你自己安排,譬如说我们按新闻在表中的id来命名 html文件名
Server.Execute("a.aspx?id="+newid, new StreamWriter(Server.MapPath(newid+".html")));或者按当前时间来命名
string nowtime=....
Server.Execute("a.aspx, new StreamWriter(Server.MapPath(nowtime+".html")));
那么此时a.aspx如何显示最新一条记录呢.
在a.aspx的SQL命令中用 select top 1 * from table order by ID DESC
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;namespace BLL
{
public class BLLToHtml
{
/// <summary>
/// 根据模版生成Html文件
/// </summary>
/// <returns></returns>
public static string CreatByTemplate(string templatePath,string savePath,string content)
{
string mbPath = templatePath;
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//读取
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd(); }
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
//根据时间自动重命名,扩展名也可以自行修改
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
str = str.Replace("$content$", content);//替换content //生成静态文件
try
{
sw = new StreamWriter(savePath + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
}
return fileName;
}
/// <summary>
/// 根据url地址生成Html文件
/// </summary>
/// <param name="url"></param>
public static bool CreateByAddress(string url,string savaPath)
{
Encoding code = Encoding.GetEncoding("utf-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null; //读取远程路径
WebRequest temp = WebRequest.Create(url);
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);
//读取
try
{
sr = new StreamReader(myTemp.GetResponseStream(), code);
str = sr.ReadToEnd(); }
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm"; //写入
try
{
sw = new StreamWriter(savaPath + fileName, false, code);
sw.Write(str);
sw.Flush(); }
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
}
return true; }
}
}
StreamWriter r=new StreamWriter(Server.MapPath(""), false,System.Text.Encoding.UTF8);
HtmlTextWriter h=new HtmlTextWriter(r);
base.Render(h);
r.Close();
h.Close();
}
StringWriter wr = new StringWriter();
Server.Execute("", wr);
this.lit.Text = Server.HtmlEncode(wr.ToString());
File.WriteAllText(Server.MapPath(""), wr.ToString());
读取模板文件,使用字符串替换的方法
/// 生成静态页
/// </summary>
/// <param name="news"></param>
/// <param name="fileName">文件存储位置</param>
/// <param name="modelPath">模板位置</param>
/// <returns>创建成功</returns>
private bool CreateHTML(NewsInfo news, string fileName, string modelPath,int id)
{
string newsFileName = sBuilder.ToString();
fileName = fileName + newsFileName;
news.NewsLink = newsFileName;
bool flag = false;
System.IO.StreamReader streamR = null;
System.IO.StreamWriter streamW = null;
string temp = string.Empty;
try
{
streamR = new System.IO.StreamReader(modelPath, Encoding.UTF8);
temp = streamR.ReadToEnd();
streamR.Close();
streamR.Dispose();
}
catch
{
streamR.Close();
streamR.Dispose();
}
if (!String.IsNullOrEmpty(temp))
{
temp = temp.Replace("#NewsId#", id.ToString());
temp = temp.Replace("#NewsPageTilte#", news.NewsTitle);
temp = temp.Replace("#NewsTitle#", news.NewsTitle);
temp = temp.Replace("#NewsAuthor#", news.NewsAuthorName);
temp = temp.Replace("#NewsDate#", news.NewsDate);
temp = temp.Replace("#NewsTimes#", news.NewsTimes.ToString());
temp = temp.Replace("#NewsContent#", news.NewsContent);
temp = temp.Replace("#NewsCategory#", news.NewsCategory);
try
{
if(System.IO.File.Exists(fileName))//判断文件是否存在
{
System.IO.File.Delete(fileName);//删除存在文件
}
streamW = new System.IO.StreamWriter(fileName, false, Encoding.UTF8);
streamW.Write(temp);
streamW.Close();
streamW.Dispose();
flag = true;
}
catch
{
streamW.Close();
streamW.Dispose();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//源码是替换掉模板中的特征字符 string mbPath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = null; //读取
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd(); }
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
} //根据时间自动重命名,扩展名也可以自行修改
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
str = str.Replace("$title$", txtTitle.Text);//替换Title
str = str.Replace("$content$", txtContent.Text);//替换content //生成静态文件
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush(); }
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
} }
//根据Url地址生成静态页保持
protected void Button2_Click(object sender, EventArgs e)
{
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = null; //读取远程路径
WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);
//读取
try
{
sr = new StreamReader(myTemp.GetResponseStream(), code);
str = sr.ReadToEnd(); }
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm"; str = str.Replace("$title$", txtTitle.Text);//替换Title
str = str.Replace("$content$", txtContent.Text);//替换content
//写入
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush(); }
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
} }