All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET开发经验(2):ASP.NET中的一些图形处理_[Asp.Net教程]
ASP.NET2.0中Treeview,的Checkboxes选中根枝叶全选中的实现方法_.net资料_编程技术
如何将应用程序由ASP.NET,1.1移植到ASP.NET,2.0_.net资料_编程技术
c#中GDI+图形图像:GDI+中的区域使用方法
小结:PHP动态网页程序两个有用的小技巧_php资料_编程技术
SQL,Server,2005数据库升级要点和技巧_[SQL,Server教程]
delphi设置代码模板
SQL数据库高级教程:SQL CREATE VIEW 语句_[SQL Server教程]
Sql Server 和 Access 操作数据库结构Sql语句_[SQL Server教程]
用gridview显示来自excel表格里的数据_[Asp.Net教程]
Reading number is top 10 pictures
Sora aoi calligraphy show
这才是真正的人体艺术2
The little woman's bright wire2
如果我是导演...
Ashlynn Brooke show proud chest measurement3
锄禾日了几个人?
全身蕾丝丝质美臀
哥斯达黎加的门将是如何练成的
Exquisite decoration is not paying too much2
XuRe xuan cool and refreshing photoes2
Download software ranking
WebService在.NET中的实战应用教学视频 → 第3集
小黑猫大战两米大花蛇
Unix video tutorial4
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
VC++6.0简体中文版
Be there or be square
The king of fighters 97(Mobile phone games-apk)
I for your crazy
网络管理员第三版
The cock of the Grosvenor LTD handsome
归海一刀 published in(发表于) 2014/1/30 0:50:55 Edit(编辑)
.net生成静态页方法总结_[Asp.Net教程]

.net生成静态页方法总结_[Asp.Net教程]

.net生成静态页方法总结_[Asp.Net教程]

最近一直忙与公司网站新闻系统的重建工作,对于大中型新闻系统的架构也做了一定的了解,服务器缓存/URL重写什么的也做了一定了解.
   现在根据公司要求是把新闻页生成静态页来处理,生成程序采用过几种方法,归纳一下几种方法的用法与区别.
   第1种方法:
用server.Execute(path As String,writer As Sysetem.IO.TextWriter) 方法,这种方法很简单,向服务器放松动态网页请求,获取页面的客户端html代码,然后把内容写进文件里.这种方法写起来比较简单: 1 Dim swHtml As StringWriter = New StringWriter()
2 Server.Execute("http://localhost/newsSzhome/manage/newstemplate.aspx, swHtml)
3 Dim strContent As String = swHtml.ToString()
4
5 Dim filepath As String = "d//news//001.html"
6 If Not (System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filepath))) Then
7 System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filepath))
8 End If
9 Dim sw As StreamWriter = New StreamWriter(filepath, False, System.Text.Encoding.Default)
10 Try
11 sw.Write(strContent )
12 Catch ex As Exception
13 Throw ex
14 Finally
15 sw.Flush()
16 sw.Close()
17 End Try   


这种方法是必须读网页地址,缺点显而易见:速度慢。另外如果请求的动态页面有验证控件的话,返回的html页面却无法进行数据验证。如果在同一个项目里的程序用这个还是很好的,但是如果是要把生成程序跟网页程序分开(如写成webservice)的话,用这个方法就相当与去打开一个外网网页,效率肯定会大打折扣(而且我在webservice上用这方法根本运行不了,程序出异常!具体原因没有去探索,估计应该是权限的问题).

   第2种方法:
这个方法跟第1种方法相似(也是需要读取网页内容),用System.Net.WebRequest.Create(path As String)方法建里一个需要读取的网页的webRequest,再获得它的WebResponse,再以流的形式写入文件.
System.Net.WebRequest 代码实例
1Dim wReq As System.Net.WebRequest = System.Net.WebRequest.Create("http://localhost/newsSzhome/manage/newstemplate.aspx"
2 Dim wResp As System.Net.WebResponse = wReq.GetResponse
3 Dim srs As System.IO.Stream = wResp.GetResponseStream
4 Dim sr As System.IO.StreamReader = New System.IO.StreamReader(srs, System.Text.Encoding.Default) 'GetEncoding("gb2312"))
5 Dim strContent As String = sr.ReadToEnd()
6Dim filepath As String = "d://news//0001.html"
7 If Not (System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filepath))) Then
8 System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filepath))
9 End If
10 Dim sw As StreamWriter = New StreamWriter(filepath, False, System.Text.Encoding.Default)
11 Try
12 sw.Write(temp)
13 Catch ex as Exception
14 Throw ex
15 Finally
16 sw.Flush()
17 sw.Close()
18 End Try


效果就不多说了,跟第1种方法问题一样!(但是我在webservice中用上面这个方法生成时还是可以成功的,但是速度慢很多.)


  第3种,就是最常用也最实用的字符替代方法String.Replace(),从文件读取模版,替换模版中的参数后输出文件,这种方法的生成速度上比第一种要快许多,而且模版内容可以用工具任意编辑
主要代码:
String.Replace方法
1 Dim sr As New System.IO.StreamReader("d://newsDetail_template.htm", System.Text.Encoding.Default)
2 Dim temp As String = sr.ReadToEnd()
3 temp = temp.Replace("@_CREATEDATE_@", DateTime.Now.ToString)
4 Dim filepath As String = "d://news//001.html"
5 If Not (System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filepath))) Then
6 System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filepath))
7 End If
8 Dim sw As StreamWriter = New StreamWriter(filepath, False, System.Text.Encoding.Default)
9 Try
10 sw.Write(temp)
11 Catch
12 Return false
13 Finally
14 sw.Flush()
15 sw.Close()
16 End Try


这个方法读取的是硬盘里的纯文件类型,在程序后台查询数据去替换掉模板文件里的特定字符.

来源:http://www.cnblogs.com/micheng11







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.