ASP #include
服务器端包含的语法、virtual 与 file 的区别、常见用法与坑。
两种写法
<!-- #include virtual="/includes/header.asp" -->
<!-- #include file="header.asp" -->| Form | Path base | Use when |
|---|---|---|
virtual | 站点根目录 | 跨目录引用,最常用 |
file | 当前文件所在目录 | 同目录下的片段 |
file="../x" | 需要开启「启用父路径」 | 尽量避免 |
virtual with a leading / is rooted at the site and most stable; file uses relative paths that break when a file moves. ../ under file also needs "Enable Parent Paths" in IIS, which is off by default.
典型结构
/includes/
├── header.asp ← 页头:logo、导航
├── footer.asp ← 页脚:版权、备案
└── conn.asp ← 数据库连接串
/default.asp
/products/list.asp<%-- default.asp --%>
<!-- #include virtual="/includes/header.asp" -->
<main>
<h1>首页</h1>
</main>
<!-- #include virtual="/includes/footer.asp" -->被包含文件里不能有什么
<%-- includes/header.asp:不要写 @ 指令 --%>
<%
' 只写片段逻辑
Dim siteName
siteName = "我的站点"
%>
<header>
<a href="/"><%= siteName %></a>
</header>Directives like <%@ Language %> may appear only once after merging; repeating them in an included file raises a duplicate-directive error. Keep all directives in the main file.
常见坑
- 变量重复声明 —— 两个片段都
Dim conn,合并后报「名称重定义」 - 输出在 include 之前 —— 有任何字节输出,
Response.Redirect就失效 - UTF-8 BOM —— 被包含文件带 BOM 会往响应里塞字节
- 改了没生效 —— IIS 缓存了编译结果
Response.Redirect and Response.Cookies fail silently — very time-consuming to diagnose.包含 vs 函数库
<%-- includes/funcs.asp:只放函数,不放输出 --%>
<%
Function SafeText(s)
SafeText = Server.HTMLEncode(s)
End Function
Function Money(v)
Money = FormatNumber(v, 2)
End Function
%><%-- 页面顶部引入函数库 --%>
<!-- #include virtual="/includes/funcs.asp" -->
<%
Response.Write Money(1234.5)
%>1,234.50Collect helpers in one funcs.asp and include it everywhere — less chance of forgetting Server.HTMLEncode than typing it by hand. Keep output fragments (header/footer) separate from logic fragments (helpers).
两种写法
<!-- #include virtual="/includes/header.asp" -->
<!-- #include file="header.asp" -->| 写法 | 路径基准 | 适用 |
|---|---|---|
| 虚拟路径 | 站点根目录 | 跨目录引用,最常用 |
| 文件路径 | 当前文件所在目录 | 同目录下的片段 |
| 父目录 | 需要开启「启用父路径」 | 尽量避免 |
virtual 以 / 开头表示站点根,最稳;file 用相对路径,文件一挪位置就断。file 里的 ../ 还需要 IIS 开启「启用父路径」,默认是关的。
典型结构
/includes/
├── header.asp ← 页头:logo、导航
├── footer.asp ← 页脚:版权、备案
└── conn.asp ← 数据库连接串
/default.asp
/products/list.asp<%-- default.asp --%>
<!-- #include virtual="/includes/header.asp" -->
<main>
<h1>首页</h1>
</main>
<!-- #include virtual="/includes/footer.asp" -->被包含文件里不能有什么
<%-- includes/header.asp:不要写 @ 指令 --%>
<%
' 只写片段逻辑
Dim siteName
siteName = "我的站点"
%>
<header>
<a href="/"><%= siteName %></a>
</header><%@ Language %> 这类指令在合并后只能出现一次。被包含的文件里再写一遍会报「重复指令」错误。所有指令都放在主文件顶部。
常见坑
- 变量重复声明 —— 两个片段都
Dim conn,合并后报「名称重定义」 - 输出在 include 之前 —— 有任何字节输出,
Response.Redirect就失效 - UTF-8 BOM —— 被包含文件带 BOM 会往响应里塞字节
- 改了没生效 —— IIS 缓存了编译结果
Response.Redirect、Response.Cookies 全部静默失效——排查起来极其费时。包含 vs 函数库
<%-- includes/funcs.asp:只放函数,不放输出 --%>
<%
Function SafeText(s)
SafeText = Server.HTMLEncode(s)
End Function
Function Money(v)
Money = FormatNumber(v, 2)
End Function
%><%-- 页面顶部引入函数库 --%>
<!-- #include virtual="/includes/funcs.asp" -->
<%
Response.Write Money(1234.5)
%>1,234.50把函数集中到一个 funcs.asp,全站包含它,比自己写一遍 Server.HTMLEncode 更不容易漏。输出型的片段(页头页脚)和逻辑型的片段(函数库)要分开文件。