当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET生成静态网页的方法

ASP.NET
安装IE补丁后ASP.NET将无法运行
在ASP.Net中应用Javascript
用ASP.NET 1.1 新特征防止Script攻击
ASP.NET中在线用户统计的简单实现及讨论
ASP.NET中将数据输出到Excel
在ASP.NET中从SQL Server检索图片
ASP.NET系统用户权限设计与实现
利用ASP.NET技术动态生成HTML页面
大数量查询分页显示 微软的解决办法
ASP.NET WEB页面多语言支持解决方案
ASP.NET 2.0里轻松获取数据库连接统计数据
ASP.NET通过DSO访问分析服务器的权限问题
ASP实现禁止从外部提交数据
Asp.Net 使用 GDI+ 绘制3D饼图入门篇源码
在ASP.NET中点击一个按钮后让它变灰的简单方法
利用JS在asp.net中实现左导航页的隐藏
asp.net中一次更新DATAGRID中所有记录
用Asp.net屏蔽F5、Ctrl+N、Alt+F4
asp.net中用C#实现站点计数器用户控件
认识ASP.NET配置文件Web.config

ASP.NET生成静态网页的方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 112 ::
收藏到网摘: n/a

环境:Microsoft .NET Framework SDK v1.1

OS:Windows Server 2003 中文版

ASP.Net生成静态HTML页,在Asp中实现的生成静态页用到的FileSystemObject对象,在.Net中涉及此类操作的是System.IO

注:以下是参考别人的程序代码,非原创!

Code:

以下为引用的内容:
/生成HTML页
public static bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}

string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 写文件
try
{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;  

此函数放在Conn.CS基类中了
在添加新闻的代码中引用 注:工程名为Hover

if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}

模板页Text.html代码

Code:

以下为引用的内容:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ShowArticle</title>
<body>
biaoti
<br>
content<br>
author
</body>
</HTML>
biaoti
<br>
content<br>
author
</body>
</HTML>

提示添加成功后会出以当前时间为文件名的html文件!上面只是把传递过来的几个参数直接写入了HTML文件中,在实际应用中需要先添加数据库,然后再写入HTML文件