動態轉靜態最直觀的做法:利用 #include 將目標 .asp 的執行結果捕獲到 `
makeasp2html.asp,內嵌表單,``index.html
<%@ Language=VBScript CodePage=65001 %>
<% Option Explicit %>
<html><head><title>ASP 轉靜態 HTML 產生器</title></head><body>
<h2>生成靜態首頁 index.html</h2>
<form method="post" action="">
<textarea name="asp2html" style="display:none"><!--#include file="index.asp"--></textarea>
<input type="submit" value="生成 HTML 頁面" />
</form>
<%
If Request.Form("asp2html") <> "" Then
Dim fso, fout, filename, content
filename = "index.html"
content = Request.Form("asp2html")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(filename), True, True) ' True=覆寫, True=Unicode
fout.Write content
fout.Close
Set fout = Nothing
Set fso = Nothing
Response.Write "✅ 成功生成 " & filename & "!點此檢視
"
End If
%>
</body></html>
| 優點 | 缺點 |
|---|---|
| 極簡:無模板語法、無替換邏輯 | 需手動點擊生成,無法自動觸發 |
| 所見即所得:含 CSS/JS/圖片完整輸出 | 動態資料 (如 Session、QueryString) 會被「凍結」在生成當下 |
| 相容性高:任何 ASP 頁面皆可用 | 大頁面 textarea 可能超過表單大小限制 (需調整 `AspMaxRequestEntityAllowed`) |
Response.Buffer = True
Server.Execute "index.asp"
content = Response.Output
Response.Clear
' 再寫檔...
article_123.html「Include 捕獲 + FSO 寫檔」是 ASP 生成靜態頁最低門檻的方法。理解其本質是**伺服端渲染結果的序列化存檔**,即可靈活應用於各種靜態化需求。若追求全自動、高效能,建議升級至 ASP.NET / PHP / Node.js 等現代框架配合靜態站點生成器 (SSG)。