asp教程.net生成静态页面代码
private static void createfile(string userid, string filename, string htmlcode)
{
filename += ".html";
string localpath = "e:/www.111com.net/sun/sundecorativesystem/users/" + userid;
if (!directory.exists(localpath))
{
directory.createdirectory(localpath);
}
localpath += "/" + filename;
if (file.exists(localpath))
{
file.delete(localpath);
}
filestream fs = new filestream(localpath, filemode.createnew);
streamwriter sw = new streamwriter(fs);
sw.autoflush = true;
htmlcode = htmlcode.replace("+", """);
sw.write(htmlcode);
sw.flush();
sw.close();
fs.close();}
//下面来看一款完整理的asp.net教程生成html代码实例吧。
环境:microsoft .net framework sdk v1.1
os:windows server 2003 中文版
asp.net生成静态html页
在asp中实现的生成静态页用到的filesystemobject对象!
在.net中涉及此类操作的是system.io
以下是程序代码 注:此代码非原创!参考别人代码
//生成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代码
-------------------------------------------------------------------------
showarticle
biaoti
content
author
萤火突击德尔金4级头盔怎么获得 萤火突击德尔金4级头盔防具一览
最新数字货币交易APP排行TOP10-前10权威交易平台排行推荐币安交易所下载
魔兽世界wlk2024美酒节任务怎么接取 魔兽世界wlk2024美酒节任务接取位置攻略
cf2024年10月藏宝阁活动在哪参加 cf2024年10月藏宝阁活动入口地址一览
最热门的现货交易平台有哪些-前10热门交易平台排行推荐币安app下载
魔兽世界国服坐骑打折促销活动什么时候开始 魔兽世界国服坐骑打折促销活动介绍
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔