欢迎光临
我们一直在努力

aspx网页以HTML形式存储的几个方法-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

第一种是模版替换:
环境: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代码

<!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文件.
第二种:
webrequest访问aspx页面,然后获取response流,得到的就是html
private void button1_click(object sender, system.eventargs e)
{
  textbox1.text=this.geturlvalue(“http://yoururl”);
}
//使用httpwebrequest获得url的返回值
public string  geturlvalue(string url)
{
system.net.webrequest       httpwebrequest=system.net.webrequest.create(url);
system.net.webresponse      httpwebresponse =httpwebrequest.getresponse();
system.io.streamreader sr=new system.io.streamreader(httpwebresponse.getresponsestream(), system.text.encoding.getencoding(“gb2312”));
            return sr.readtoend();
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » aspx网页以HTML形式存储的几个方法-.NET教程,Asp.Net开发
分享到: 更多 (0)