asp中靜態(tài)頁面實現(xiàn)方法
1、使用isapi_rewrite進(jìn)行動態(tài)鏈接重寫html靜態(tài)網(wǎng)址。isapi_rewrite是一個dll組件,re_write是iis里的一個模塊。這個篩選器實現(xiàn)是通過正則表達(dá)式,將動態(tài)網(wǎng)頁網(wǎng)址映射成為靜態(tài)網(wǎng)址。如可將news.asp?id=95通過re_write將其轉(zhuǎn)換成news/95.html。映射的正則表達(dá)式在httpd.ini文件里進(jìn)行設(shè)置。
舉個小小例:處理數(shù)據(jù)翻頁,那么寫法是:
more_<%=page%>_<%=type%>.html (注:page是翻頁頁數(shù),type是數(shù)據(jù)類型)表現(xiàn)形式:more_1_95.html
如果翻下一頁,則為:more_2_95.html,繼續(xù)下一頁的循環(huán),則是:
more_3_95.html,以此類推。
不過你需要在httpd.ini文件中增加以下代碼:
rewriterule /more_(d+)_(d+).html /jsp教程/more.asp?page=$1&type=$2 [n,i] 字串9
如果你的動態(tài)程序有多個參數(shù)需要傳遞,那么就增加多個(d+)即可,如下:
rewriterule /more_(d+)_(d+)_(d+).html /asp/more.asp?page=$1&type=$2&type2=$3 [n,i]
優(yōu)點(diǎn):在程序上基本不需做什么變化。麻煩:要實現(xiàn)這個需要對iis進(jìn)行控制,所以當(dāng)你租用別人的服務(wù)器時,則需要先跟服務(wù)商聯(lián)系。(當(dāng)然這個是對asp而言,asp.net教程就不用——直接將dll程序集放到程序中的bin再適當(dāng)?shù)呐渲眉纯蓪崿F(xiàn))
2、iis的404錯誤處理機(jī)制:通過自定義錯誤,轉(zhuǎn)向我們準(zhǔn)備好的處理頁。不過這種可拓展性有待研究,對程序處理的統(tǒng)籌要求也高,不大適合實際應(yīng)用的樣子。
首先,設(shè)置站點(diǎn)屬性-自定意錯誤
找到http錯誤404,然后編輯屬性->消息類型選中url->url填入"/index.asp",或您的錯誤處理頁面.
這樣,比如用戶或蜘蛛訪問http://cn/12345.html 時(12345為文章在數(shù)據(jù)庫教程的id).由于些頁面不存在,所以觸發(fā)了404錯誤.轉(zhuǎn)向了index.asp
在index.asp里加
currdomain=request.servervariables("http_host") '當(dāng)前訪問域名
currurl=replace(request.servervariables("query_string"),"404;http://"&currdomain&":80","") '當(dāng)前訪問url
此時的currurl應(yīng)該是:12345.html .
3.
1.新建一個文件夾info (因為最終訪問信息的頁面url為http://localhost/info/?1.html)
2.在info文件夾下新建一個default.asp文件(就是默認(rèn)首頁的那個頁面)
default.asp文件的內(nèi)容如下
<%
currdomain=request.servervariables("http_host") '當(dāng)前訪問域名
currurl=replace(request.servervariables("query_string"),"404;http://"&currdomain&"/info/?","") '當(dāng)前訪問url
id=replace(currurl,".html","")
%>
其中id即是傳入的參數(shù)
如果是多個參數(shù)可以把url偽靜態(tài)化為info/?1-2-3.html
其中1,2,3各代表三個參數(shù)的值,分隔字符串分別提出即可。
真實html靜態(tài)頁面
把html代碼寫入到文件中然后生成.html格式的文件
<%
filename="test.htm"
if request("body")<>"" then
set fso = server.createobject("scripting.filesystemobject")
set htmlwrite = fso.createtextfile(server.mappath(""filename""))
htmlwrite.write "<html><head><title>" request.form("title") "</title></head>"
htmlwrite.write "<body>輸出title內(nèi)容: " request.form("title") "<br /> 輸出body內(nèi)容:" request.form("body") "</body></html>"
htmlwrite.close
set fout=nothing
set fso=nothing
end if
%>
<form name="form" method="post" action="">
<input name="title" value="title" size=26>
<br>
<textarea name="body">body</textarea>
<br>
<br>
<input type="submit" name="submit" value="生成html">
</form>
2、但是按照上面的方法生成html文件非常不方便,第二種方法就是利用模板技術(shù),將模板中特殊代碼的值替換為從表單或是數(shù)據(jù)庫字段中接受過來的值,完成模板功能;將最終替換過的所有模板代碼生成html文件.這種技術(shù)采用得比較多,大部分的cms都是使用這類方法.
template.htm ' //模板文件
<html>
<head>
<title>$title$ by aspid.cn</title>
</head>
<body>
$body$
</body>
</html>testtemplate.asp '// 生成html
<%
dim fso,htmlwrite
dim strtitle,strcontent,strout
'// 創(chuàng)建文件系統(tǒng)對象
set fso=server.createobject("scripting.filesystemobject")
'// 打開網(wǎng)頁模板文件,讀取模板內(nèi)容
set htmlwrite=fso.opentextfile(server.mappath("template.htm"))
strout=f.readall
htmlwrite.close
strtitle="生成的網(wǎng)頁標(biāo)題"
strcontent="生成的網(wǎng)頁內(nèi)容"
'// 用真實內(nèi)容替換模板中的標(biāo)記
strout=replace(strout,"$title$",strtitle)
strout=replace(strout,"$body$",strcontent)
'// 創(chuàng)建要生成的靜態(tài)頁
set htmlwrite=fso.createtextfile(server.mappath("test.htm"),true)
'// 寫入網(wǎng)頁內(nèi)容
htmlwrite.writeline strout
htmlwrite.close
response.write "生成靜態(tài)頁成功!"
'// 釋放文件系統(tǒng)對象
set htmlwrite=nothing
set fso=nothing
%>
相關(guān)文章
為什么 Windows2003 的 IIS6.0 不能上傳超過 200K 的文件?
為什么 Windows2003 的 IIS6.0 不能上傳超過 200K 的文件?...2006-12-12ASP字符串大寫轉(zhuǎn)換成小寫 ASP小寫轉(zhuǎn)換成大寫 ucase lcase
字符串大寫字面轉(zhuǎn)換成小寫字面或者小寫字面轉(zhuǎn)換成大寫字面,ASP(特指VBS)里自帶了兩個函數(shù)2009-06-06