生成html靜態頁

2021-09-22 09:41:39 字數 4102 閱讀 4925

方案1: 

///

/// 傳入url返回網頁的html** 

///

/// url

///

public static string geturltohtml(string url)   

// get the response stream. 

system.io.stream respstream = wresp.getresponsestream(); 

// dim reader as streamreader = new streamreader(respstream) 

system.io.streamreader reader = new system.io.streamreader(respstream, system.text.encoding.getencoding("gb2312")); 

return reader.readtoend(); 

} catch(system.exception ex)   

return ""; 

} 你可以用這個函式獲取網頁的客戶端的html**,然後儲存到.html檔案裡就可以了。 

方案2: 

生成單個的靜態頁面不是難點,難的是各個靜態頁面間的關聯和鏈結如何保持完整; 

特別是在頁面頻繁更新、修改、或刪除的情況下; 

像阿里巴巴的頁面也全部是html的,估計用的是位址對映的功能 

可以看看這個頁面,分析一下他的「競價倒計時」功能 

asp.net生成靜態html頁 

在asp中實現的生成靜態頁用到的filesystemobject物件! 

在.net中涉及此類操作的是system.io 

以下是程式** 注:此**非原創!參考別人** 

code: 

//生成html頁 

public static bool writefile(string strtext,string strcontent,string strauthor)   

catch(exception exp)   

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   

catch(exception ex)   

finally   

return true; 

此函式放在conn.cs基類中了 

在新增新聞的**中引用 注:工程名為hover 

if(hover.conn.writefilethis.title.text.tostring),this.content.text.tostring),this.author.text.tostring)))   

else   

模板頁text.html** 

code:   

showarticle

biaoti 

content

author 

biaoti 

content

author 

提示新增成功後會出以當前時間為檔名的html檔案!上面只是把傳遞過來的幾個引數直接寫入了html檔案中,在實際應用中需要先新增資料庫,然後再寫入html檔案 

方案3: 

給乙個客戶端參考的例子(sj) 

它的作用在於以客戶端的方式獲取某個頁面的**,然後可以做為其他用途,本例是直接輸出 

方案4:學csdn一樣。用xml儲存資料,模版xsl也只有乙個檔案。 

使用xml來儲存資料,使用xsl來定義模板並且生稱資料。可以通過xsl來很方便的在客戶端或者服務段顯示資料。如果要生成靜態葉面那更簡單了。去查一下.net的xml類包問題解決。 

優點:可以方便快速轉換成你想要的格式和內容。 

方案5: 

思路 1. 利用如dw-mx這樣的工具生成html格式的模板,在需要新增格式的地方加入特殊標記(如$htmlformat$),動態生成檔案時利用**讀取此模板,然後獲得前台輸入的內容,新增到此模板的標記位置中,生成新檔名後寫入磁碟,寫入後再向資料庫中寫入相關資料。 

2. 使用後台**硬編碼html檔案,可以使用htmltextwriter類來寫html檔案。 

優點 1. 可以建立非常複雜的頁面,利用包含js檔案的方法,在js檔案內加入document.write()方法可以在所有頁面內加入如頁面頭,廣告等內容。 

2. 靜態html檔案利用ms windows2000的index server可以建立全文搜尋引擎,利用asp.net可以以datatable的方式得到搜尋結果。而win2000的index服務無法查詢xml檔案的內容。如果包括了資料庫搜尋與index索引雙重查詢,那麼此搜尋功能將非常強大。 

3. 節省伺服器的負荷,請求乙個靜態的html檔案比乙個aspx檔案伺服器資源節省許多。 

缺點 思路二: 如果用硬編碼的方式,工作量非常大,需要非常多的html**。除錯困難。而且使用硬編碼生成的html樣式無法修改,如果**更換樣式,那麼必須得重新編碼,給後期帶來巨大的工作量。 

因此這裡採用的是第一種思路 

示列** 

1.定義(template.htm)html模板頁面 

//---------------------讀html模板頁面到stringbuilder物件裡---- 

string format=new string[4];//定義和htmlyem標記數目一致的陣列 

stringbuilder htmltext=new stringbuilder(); 

try   

sr.close(); 

} } 

catch   

//---------------------給標記陣列賦值------------ 

format[0]="background="bg.jpg"";//背景 

format[1]= "#990099";//字型顏色 

format[2]="150px";//字型大小 

format[3]= "<marquee>生成的模板html頁面</marquee>";//文字說明 

//----------替換htm裡的標記為你想加的內容 

for(int i=0;i<4;i++)   

//----------生成htm檔案------------------―― 

try   

} catch   

小結 用此方法可以方便的生成html檔案。程式使用了是迴圈替換,因此對需替換大量元素的模板速度非常快。

分類: 

生成html靜態頁

方案1 傳入url返回網頁的html url public static string geturltohtml string url get the response stream.system.io.stream respstream wresp.getresponsestream dim re...

生成html靜態頁

方案1 傳入url返回網頁的html url public static string geturltohtml string url get the response stream.system.io.stream respstream wresp.getresponsestream dim re...

ASPX頁生成靜態HTML頁

商品detail資訊頁面都是html頁面,減少訪問資料庫的次數,提高效能 方案1 傳入url返回網頁的html url public static string geturltohtml string url get the response stream.system.io.stream resp...