<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[如何生成静态页面的五种方案]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[代码编程]]></category>
<pubDate>Mon, 11 Dec 2006 09:11:35 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	　　方案1：<br/><div class="code"><br/><br/>/// &lt;summary&gt;<br/>/// 传入URL返回网页的html代码<br/>/// &lt;/summary&gt;<br/>/// &lt;param name=&quot;Url&quot;&gt;URL&lt;/param&gt;<br/>/// &lt;returns&gt;&lt;/returns&gt;<br/>public static &nbsp;string getUrltoHtml(string Url)<br/>&#123;<br/>errorMsg = &quot;&quot;;<br/>try<br/>&#123;<br/>System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);<br/>// Get the response instance.<br/>System.Net.WebResponse wResp =wReq.GetResponse();<br/>// Read an HTTP-specific property<br/>//if (wResp.GetType() ==HttpWebResponse)<br/>//&#123;<br/>//DateTime updated &nbsp;=((System.Net.HttpWebResponse)wResp).LastModified;<br/>//&#125;<br/>// Get the response stream.<br/>System.IO.Stream respStream &nbsp;= wResp.GetResponseStream();<br/>// Dim reader As StreamReader = New StreamReader(respStream)<br/>System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding(&quot;gb2312&quot;));<br/>return &nbsp;reader.ReadToEnd();<br/><br/>&#125;<br/>catch(System.Exception ex)<br/>&#123;<br/>errorMsg = ex.Message ;<br/>&#125;<br/>return &quot;&quot;;<br/>&#125;<br/><br/></div><br/><br/>　　你可以用这个函数获取网页的客户端的html代码，然后保存到.html文件里就可以了。<br/><br/>　　方案2：<br/><br/>　　生成单个的静态页面不是难点，难的是各个静态页面间的关联和链接如何保持完整；特别是在页面频繁更新、修改、或删除的情况下；<br/><br/>　　像阿里巴巴的页面也全部是html的，估计用的是地址映射的功能关于地址映射可参考：<a href="http://www.easewe.com/Article/ShowArticle.aspx?article=131" target="_blank">http://www.easewe.com/Article/ShowArticle.aspx?article=131</a><br/><br/>　　可以看看这个页面，分析一下他的“竞价倒计时”功能<a href="http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5" target="_blank">http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5</a><br/><br/>　　ASP.Net生成静态HTML页<br/>　　在Asp中实现的生成静态页用到的FileSystemObject对象!<br/>　　在.Net中涉及此类操作的是System.IO <br/>　　以下是程序代码 注:此代码非原创!参考别人代码<br/><br/> &nbsp;<div class="code"> &nbsp; <br/>//生成HTML页<br/> &nbsp;public static bool WriteFile(string strText,string strContent,string strAuthor) <br/> &nbsp;&#123;<br/> &nbsp; string path = HttpContext.Current.Server.MapPath(&quot;/news/&quot;);<br/> &nbsp; Encoding code = Encoding.GetEncoding(&quot;gb2312&quot;);<br/> &nbsp; // 读取模板文件<br/> &nbsp; string temp = HttpContext.Current.Server.MapPath(&quot;/news/text.html&quot;);<br/> &nbsp; StreamReader sr=null;<br/> &nbsp; StreamWriter sw=null;<br/> &nbsp; string str=&quot;&quot;; &nbsp;<br/> &nbsp; try<br/> &nbsp; &#123;<br/> &nbsp; &nbsp;sr = new StreamReader(temp, code);<br/> &nbsp; &nbsp;str = sr.ReadToEnd(); // 读取文件<br/> &nbsp; &#125;<br/> &nbsp; catch(Exception exp)<br/> &nbsp; &#123;<br/> &nbsp; &nbsp;HttpContext.Current.Response.Write(exp.Message);<br/> &nbsp; &nbsp;HttpContext.Current.Response.End();<br/> &nbsp; &nbsp;sr.Close();<br/> &nbsp; &#125;<br/> &nbsp;<br/> &nbsp; <br/> &nbsp; string htmlfilename=DateTime.Now.ToString(&quot;yyyyMMddHHmmss&quot;)+&quot;.html&quot;;<br/> &nbsp; // 替换内容<br/> &nbsp; // 这时,模板文件已经读入到名称为str的变量中了<br/> &nbsp; str =str.Replace(&quot;ShowArticle&quot;,strText); //模板页中的ShowArticle<br/> &nbsp; str = str.Replace(&quot;biaoti&quot;,strText);<br/> &nbsp; str = str.Replace(&quot;content&quot;,strContent);<br/> &nbsp; str = str.Replace(&quot;author&quot;,strAuthor);<br/> &nbsp; // 写文件<br/> &nbsp; try<br/> &nbsp; &#123;<br/> &nbsp; &nbsp;sw = new StreamWriter(path + htmlfilename , false, code);<br/> &nbsp; &nbsp;sw.Write(str);<br/> &nbsp; &nbsp;sw.Flush();<br/> &nbsp; &#125;<br/> &nbsp; catch(Exception ex)<br/> &nbsp; &#123;<br/> &nbsp; &nbsp;HttpContext.Current.Response.Write(ex.Message);<br/> &nbsp; &nbsp;HttpContext.Current.Response.End();<br/> &nbsp; &#125;<br/> &nbsp; finally<br/> &nbsp; &#123;<br/> &nbsp; &nbsp;sw.Close();<br/> &nbsp; &#125;<br/> &nbsp; return true;<br/><br/>　　此函数放在Conn.CS基类中了在添加新闻的代码中引用 注：工程名为Hover<br/><br/> &nbsp; &nbsp;if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))<br/> &nbsp; &nbsp;&#123;<br/> &nbsp; &nbsp; Response.Write(&quot;添加成功&quot;);<br/> &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp;else<br/> &nbsp; &nbsp;&#123;<br/> &nbsp; &nbsp; Response.Write(&quot;生成HTML出错!&quot;);<br/> &nbsp; &nbsp;&#125; <br/><br/><br/></div><br/><br/>　　模板页Text.html代码<br/><br/><div class="code"><br/><br/>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; &gt;<br/>&lt;HTML&gt;<br/>&lt;HEAD&gt;<br/> &nbsp;&lt;title&gt;ShowArticle&lt;/title&gt;<br/> &nbsp; &lt;body&gt;<br/>biaoti<br/>&lt;br&gt;<br/>content&lt;br&gt;<br/>author<br/>&lt;/body&gt;<br/>&lt;/HTML&gt;<br/>biaoti<br/>&lt;br&gt;<br/>content&lt;br&gt;<br/>author<br/>&lt;/body&gt;<br/>&lt;/HTML&gt; <br/><br/></div><br/><br/>　　提示添加成功后会出以当前时间为文件名的html文件!上面只是把传递过来的几个参数直接写入了HTML文件中,在实际应用中需要先添加数据库，然后再写入HTML文件<br/><br/>　　方案3：给一个客户端参考的例子（SJ）<br/><br/>　　它的作用在于以客户端的方式获取某个页面的代码，然后可以做为其他用途，本例是直接输出<br/><br/><div class="code"><br/><br/>&lt;script&gt;<br/> &nbsp; &nbsp;var oXmlHttp = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br/> &nbsp; &nbsp;oXmlHttp.open(&quot;GET&quot;,&quot;http://www.webjx.com&quot;, false);<br/> &nbsp; &nbsp;oXmlHttp.send()<br/> &nbsp; &nbsp;var oStream = new ActiveXObject(&quot;ADODB.Stream&quot;);<br/> &nbsp; &nbsp;if(oStream == null)<br/> &nbsp; &nbsp; &nbsp; &nbsp;alert(&quot;您的机器不支持ADODB.Stream.&quot;)<br/> &nbsp; &nbsp;else<br/> &nbsp; &nbsp;&#123;<br/> &nbsp; &nbsp; &nbsp; &nbsp;oStream.Type=1;<br/> &nbsp; &nbsp; &nbsp; &nbsp;oStream.Mode=3;<br/> &nbsp; &nbsp; &nbsp; &nbsp;oStream.Open() ;<br/> &nbsp; &nbsp; &nbsp; &nbsp;oStream.Write(oXmlHttp.responseBody);<br/> &nbsp; &nbsp; &nbsp; &nbsp;oStream.Position= 0;<br/> &nbsp; &nbsp; &nbsp; &nbsp;oStream.Type= 2;<br/> &nbsp; &nbsp; &nbsp; &nbsp;oStream.Charset=&quot;gb2312&quot;;<br/> &nbsp; &nbsp; &nbsp; &nbsp;var result= oStream.ReadText();<br/> &nbsp; &nbsp; &nbsp; &nbsp;oStream.Close();<br/> &nbsp; &nbsp; &nbsp; &nbsp;oStream = null;<br/> &nbsp;var aa = window.open(&quot;&quot;,&quot;&quot;)<br/> &nbsp;document.write(result);<br/> &nbsp; &nbsp; &nbsp; &nbsp;aa.document.write(result);<br/> &nbsp; &nbsp;&#125;<br/>&lt;/script&gt;<br/><br/></div>　<br/>　方案4：学csdn一样。用xml保存数据，模版XSL也只有一个文件。<br/><br/>　　使用xml来保存数据，使用xsl来定义模板并且生称数据。可以通过xsl来很方便的在客户端或者服务段显示数据。如果要生成静态叶面那更简单了。去查一下.net的xml类包问题解决。<br/><br/>　　优点：可以方便快速转换成你想要的格式和内容。<br/>　　缺点：需要学习更多的内容，不好入门。<br/><br/>　　方案5：<br/><br/>　　思路 <br/><br/>　　1. 利用如Dw-Mx这样的工具生成html格式的模板，在需要添加格式的地方加入特殊标记(如$htmlformat$),动态生成文件时利用代码读取此模板，然后获得前台输入的内容，添加到此模板的标记位置中，生成新文件名后写入磁盘，写入后再向数据库中写入相关数据。<br/> &nbsp; &nbsp; &nbsp; 2. 使用后台代码硬编码Html文件，可以使用HtmlTextWriter类来写html文件。 <br/>优点 <br/><br/>　　1. 可以建立非常复杂的页面，利用包含js文件的方法，在js文件内加入document.write()方法可以在所有页面内加入如页面头，广告等内容。 <br/><br/>　　2. 静态html文件利用MS Windows2000的Index Server可以建立全文搜索引擎，利用asp.net可以以DataTable的方式得到搜索结果。而Win2000的Index服务无法查找xml文件的内容。如果包括了数据库搜索与Index索引双重查找，那么此搜索功能将非常强大。 <br/><br/>　　3. 节省服务器的负荷，请求一个静态的html文件比一个aspx文件服务器资源节省许多。 <br/><br/>　　缺点 <br/><br/>　　思路二： 如果用硬编码的方式，工作量非常大，需要非常多的html代码。调试困难。而且使用硬编码生成的html样式无法修改，如果网站更换样式，那么必须得重新编码，给后期带来巨大的工作量。 <br/><br/>　　因此这里采用的是第一种思路 <br/><br/>　　示列代码 <br/><br/>　　1.定义(template.htm)html模板页面 <br/><br/><div class="code"><br/><br/>　　＜html＞ <br/><br/>　　＜head＞ <br/><br/>　　＜title＞＜/title＞ <br/><br/>　　＜meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot;＞ <br/><br/>　　＜/head＞ <br/><br/>　　＜body ＞ <br/><br/>　　＜table $htmlformat&#91;0&#93; height=&quot;100%&quot; border=&quot;0&quot; width=&quot;100%&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; bgcolor=&quot;#eeeeee&quot; style=&quot;border:1px solid #000000&quot;＞ <br/><br/>　　＜tr＞ <br/><br/>　　＜td width=&quot;100%&quot; valign=&quot;middle&quot; align=&quot;left&quot;＞ <br/><br/>　　＜span style=&quot;color: $htmlformat&#91;1&#93;;font-size: $htmlformat&#91;2&#93;&quot;＞$htmlformat&#91;3&#93;＜/span＞ <br/><br/>　　＜/td＞ <br/><br/>　　＜/tr＞ <br/><br/>　　＜/table＞ <br/><br/>　　＜/body＞ <br/><br/>　　＜/html＞ <br/><br/></div><br/><br/>　　2.asp.net代码： <br/><div class="code"><br/>　　//---------------------读html模板页面到stringbuilder对象里---- <br/><br/>　　string&#91;&#93; format=new string&#91;4&#93;;//定义和htmlyem标记数目一致的数组 <br/><br/>　　StringBuilder htmltext=new StringBuilder(); <br/><br/>　　try <br/><br/>　　&#123; <br/><br/>　　　using (StreamReader sr = new StreamReader(&quot;存放模板页面的路径和页面名&quot;)) <br/><br/>　　　&#123; <br/><br/>　　String line; <br/><br/>　　while ((line = sr.ReadLine()) != null) <br/><br/>　　&#123; <br/><br/>　　　htmltext.Append(line); <br/><br/>　　&#125; <br/><br/>　　sr.Close(); <br/><br/>　　　&#125; <br/><br/>　　&#125; <br/><br/>　　catch <br/><br/>　　&#123; <br/><br/>　　　Response.Write(&quot;＜Script＞alert(&#039;读取文件错误&#039;)＜/Script＞&quot;); <br/><br/>　　&#125; <br/><br/>　　//---------------------给标记数组赋值------------ <br/><br/>　　format&#91;0&#93;=&quot;background=&quot;bg.jpg&quot;&quot;;//背景图片 <br/><br/>　　format&#91;1&#93;= &quot;#990099&quot;;//字体颜色 <br/><br/>　　format&#91;2&#93;=&quot;150px&quot;;//字体大小 <br/><br/>　　format&#91;3&#93;= &quot;＜marquee＞生成的模板html页面＜/marquee＞&quot;;//文字说明 <br/><br/>　　//----------替换htm里的标记为你想加的内容 <br/><br/>　　for(int i=0;i＜4;i++) <br/><br/>　　&#123; <br/><br/>　　　htmltext.Replace(&quot;$htmlformat&#91;&quot;+i+&quot;&#93;&quot;,format&#91;i&#93;); <br/><br/>　　&#125; <br/><br/>　　//----------生成htm文件------------------―― <br/><br/>　　try <br/><br/>　　&#123; <br/><br/>　　　using(StreamWriter sw=new StreamWriter(&quot;存放路径和页面名&quot;,false,System.Text.Encoding.GetEncoding(&quot;GB2312&quot;))) <br/><br/>　　&#123; <br/><br/>　　　sw.WriteLine(htmltext); <br/><br/>　　　sw.Flush(); <br/><br/>　　　sw.Close(); <br/><br/>　　&#125; <br/><br/>　　&#125; <br/><br/>　　catch <br/><br/>　　&#123; <br/><br/>　　Response.Write (&quot;The file could not be wirte:&quot;); <br/><br/>　　&#125; <br/><br/></div><br/>　　小结 <br/>　　用此方法可以方便的生成html文件。程序使用了是循环替换，因此对需替换大量元素的模板速度非常快。<br/><br/><br/><br/>Tags - <a href="http://www.dzhope.com/tags/%25E7%2594%259F%25E6%2588%2590%25E9%259D%2599%25E6%2580%2581/" rel="tag">生成静态</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] 如何生成静态页面的五种方案]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>