您的位置:首页 > 编程语言 > ASP

ASP.NET生成静态页面

2009-08-18 15:49 253 查看
ASP.NET生成静态页面,效果图如下:

1.模板样式:



2.添加内容:



3.生成的静态页面:



实现代码:

//“发表”按钮
protected void BtnPublic_Click(object sender, EventArgs e)
{
string title = TextBox1.Text;
string body = WebEditor1.Text;
string datetime = DateTime.Now.ToShortDateString();
string[] strNewHtml = { title, body, datetime };
string[] strOldHtml = { "@Title", "@Body", "@datetime" };
string strFileName = DateTime.Now.ToString("ddhhmmss") + ".html";//生成的文件名;
string strFilePath = string.Format("../Exersize/HtmlPage/{0}", strFileName);//文件路径;
if(CreateHtmlPage.CreatHtmlPage(strNewHtml,strOldHtml,"../Exersize/muban.htm",strFilePath ))
{
Response .Write("发表成功!");
Response.Redirect(strFilePath);
}
}


生成静态页面的类: CreateHtmlPage.cs


public class CreateHtmlPage
{
public CreateHtmlPage()
{

}
//参数①:新的新闻;参数②:旧的新闻;
//参数③:模板地址;参数④:生成文件地址;
public static bool CreatHtmlPage(string[] strNewsHtml, string[] strOldHtml, string strModeFilePath, string strPageFilePath)
{
bool Flage = false;
StreamReader ReaderFile = null;
StreamWriter WrirteFile = null;
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: