.net生成静态页方法总结

本文介绍了三种网页静态化的方法:通过Server.Execute执行动态页面、使用System.Net.WebRequest创建请求获取内容及利用String.Replace进行模版替换。重点对比了各方法的优缺点,特别是在速度和实用性方面的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      第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,再以流的形式写入文件.
            

 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(),从文件读取模版,替换模版中的参数后输出文件,这种方法的生成速度上比第一种要快许多,而且模版内容可以用工具任意编辑
主要代码:

 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
            这个方法读取的是硬盘里的纯文件类型,在程序后台查询数据去替换掉模板文件里的特定字符.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzhdim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值