ASP Content Linking
核对 MSWC.NextLink 内容链接组件的制表符列表文件格式、nlcode.inc 公共导航包含机制及 8 项列表索引与前后页导航方法。
学完这一章,你能够
- 核对 MSWC.NextLink 内容链接组件的制表符列表文件格式、nlcode.inc 公共导航包含机制及 8 项列表索引与前后页导航方法。
Content Linking 组件与 NextLink 导航定位
`ASP Content Linking` 组件通过 `Server.CreateObject("MSWC.NextLink")` 返回 `NextLink` 对象,用于在多页教程或连载文档之间自动生成目录列表与上一页/下一页顺序导航。
links.txt 制表符清单与 nlcode.inc 公共包含
使用该组件时,先维护一个纯文本清单文件(如 `links.txt`),每行按展示顺序写入页面 URL 与描述文本,两者之间以制表符(Tab)分隔;日后增删或调整章节次序只需编辑这一个文本文件,全站导航即自动更新。随后编写公共片段 `nlcode.inc`,在其中实例化 `MSWC.NextLink`,当 `GetListIndex("links.txt") > 1` 时输出 `Previous Page` 链接并输出 `Next Page` 链接(生产中还应补充 `GetListIndex < GetListCount` 判断避免末页输出空链接),最后在清单所列的每个 `.asp` 页面通过 `<!-- #include file="nlcode.inc" -->` 引入即可。
NextLink 全部 8 项方法行为与边界验证
`MSWC.NextLink` 提供 8 项核心方法,涵盖条目总数统计(示例输出 `There are 4 items in the list`)、当前页 1 基索引(示例 `Item number 3`,未命中返回 `0`)、下一项与上一项的描述及 URL 读取(示例指向 `ASP Variables` / `asp_variables.asp`),以及按序号直接读取第 `N` 项的描述与 URL。本地 CPython 3.12.14 离线验证脚本在临时目录中写入真实制表符分隔的 `links.txt`,验证了全部 8 项方法、未在列表中页面调用 `GetNext*`/`GetPrevious*` 回落到末项/首项的规范行为,以及首/中/末三页的导航栏渲染。
Content Linking(MSWC.NextLink)组件方法参考
| 方法 | 作用说明 | 典型调用 |
| GetListCount | 返回内容链接清单文件中包含的总条目数量。 | nl.GetListCount("links.txt") |
| GetListIndex | 返回当前页面在清单中的 1 基序号;若当前页不在清单中则返回 0。 | nl.GetListIndex("links.txt") |
| GetNextDescription | 返回下一条目的文本描述;若当前页不在清单中,返回清单最后一项的描述。 | nl.GetNextDescription("links.txt") |
| GetNextURL | 返回下一条目的 URL;若当前页不在清单中,返回清单最后一项的 URL。 | nl.GetNextURL("links.txt") |
| GetNthDescription | 按 1 基序号 N 直接返回清单中第 N 个条目的文本描述。 | nl.GetNthDescription("links.txt", 3) |
| GetNthURL | 按 1 基序号 N 直接返回清单中第 N 个条目的 URL。 | nl.GetNthURL("links.txt", 3) |
| GetPreviousDescription | 返回上一条目的文本描述;若当前页不在清单中,返回清单第一项的描述。 | nl.GetPreviousDescription("links.txt") |
| GetPreviousURL | 返回上一条目的 URL;若当前页不在清单中,返回清单第一项的 URL。 | nl.GetPreviousURL("links.txt") |
运行 MSWC.NextLink 清单解析、8 项导航方法与 nlcode.inc 边界渲染离线验证
展开查看收起
"""Offline Classic ASP Content Linking (MSWC.NextLink) verification lab (IIS ASP absent)."""
import json
from pathlib import Path
import tempfile
class MockNextLink:
"""Offline model of MSWC.NextLink reading tab-delimited Content Linking list files."""
def __init__(self, current_page: str) -> None:
self.current_page = current_page
@staticmethod
def _load(list_path: Path) -> list[tuple[str, str]]:
entries: list[tuple[str, str]] = []
for raw in list_path.read_text(encoding="utf-8").splitlines():
line = raw.strip()
if not line:
continue
parts = line.split("\t")
url = parts[0].strip()
desc = parts[1].strip() if len(parts) > 1 else ""
entries.append((url, desc))
return entries
def GetListCount(self, list_path: Path) -> int:
return len(self._load(list_path))
def GetListIndex(self, list_path: Path) -> int:
entries = self._load(list_path)
for idx, (url, _) in enumerate(entries, start=1):
if url.lower() == self.current_page.lower():
return idx
return 0
def GetNthURL(self, list_path: Path, n: int) -> str:
entries = self._load(list_path)
if 1 <= n <= len(entries):
return entries[n - 1][0]
return ""
def GetNthDescription(self, list_path: Path, n: int) -> str:
entries = self._load(list_path)
if 1 <= n <= len(entries):
return entries[n - 1][1]
return ""
def GetNextURL(self, list_path: Path) -> str:
entries = self._load(list_path)
idx = self.GetListIndex(list_path)
if idx == 0:
return entries[-1][0]
if idx < len(entries):
return entries[idx][0]
return ""
def GetNextDescription(self, list_path: Path) -> str:
entries = self._load(list_path)
idx = self.GetListIndex(list_path)
if idx == 0:
return entries[-1][1]
if idx < len(entries):
return entries[idx][1]
return ""
def GetPreviousURL(self, list_path: Path) -> str:
entries = self._load(list_path)
idx = self.GetListIndex(list_path)
if idx == 0:
return entries[0][0]
if idx > 1:
return entries[idx - 2][0]
return ""
def GetPreviousDescription(self, list_path: Path) -> str:
entries = self._load(list_path)
idx = self.GetListIndex(list_path)
if idx == 0:
return entries[0][1]
if idx > 1:
return entries[idx - 2][1]
return ""
def render_nav_bar(nl: MockNextLink, list_path: Path) -> str:
idx = nl.GetListIndex(list_path)
total = nl.GetListCount(list_path)
parts = []
if idx > 1:
parts.append(f"<a href='{nl.GetPreviousURL(list_path)}'>Previous Page</a>")
if 0 < idx < total:
parts.append(f"<a href='{nl.GetNextURL(list_path)}'>Next Page</a>")
return " ".join(parts)
LINKS_CONTENT = (
"asp_intro.asp\tASP 简介\n"
"asp_syntax.asp\tASP 语法\n"
"asp_variables.asp\tASP 变量\n"
"asp_procedures.asp\tASP 程序\n"
)
def main() -> None:
cases = []
with tempfile.TemporaryDirectory(prefix="asp_nextlink_") as tmp:
links_file = Path(tmp) / "links.txt"
links_file.write_text(LINKS_CONTENT, encoding="utf-8")
# Case 1: GetListCount and GetListIndex (listed vs unlisted)
nl3 = MockNextLink("asp_variables.asp")
nl_unlisted = MockNextLink("about.asp")
expected1 = {"count": 4, "index_variables": 3, "index_unlisted": 0}
actual1 = {
"count": nl3.GetListCount(links_file),
"index_variables": nl3.GetListIndex(links_file),
"index_unlisted": nl_unlisted.GetListIndex(links_file),
}
cases.append({
"name": "nextlink_count_and_index",
"summary": "Verify GetListCount=4, GetListIndex=3 for 3rd item, and 0 for unlisted page.",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: GetNext* & GetPrevious* for middle page and unlisted fallback
nl2 = MockNextLink("asp_syntax.asp")
expected2 = {
"next_url": "asp_variables.asp",
"next_desc": "ASP 变量",
"prev_url": "asp_intro.asp",
"prev_desc": "ASP 简介",
"unlisted_next_url": "asp_procedures.asp",
"unlisted_prev_url": "asp_intro.asp",
}
actual2 = {
"next_url": nl2.GetNextURL(links_file),
"next_desc": nl2.GetNextDescription(links_file),
"prev_url": nl2.GetPreviousURL(links_file),
"prev_desc": nl2.GetPreviousDescription(links_file),
"unlisted_next_url": nl_unlisted.GetNextURL(links_file),
"unlisted_prev_url": nl_unlisted.GetPreviousURL(links_file),
}
cases.append({
"name": "nextlink_next_and_previous_navigation",
"summary": "Verify GetNextURL/Description and GetPreviousURL/Description including unlisted fallback.",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: GetNthURL & GetNthDescription
expected3 = {
"third_url": "asp_variables.asp",
"third_desc": "ASP 变量",
"oob_url": "",
}
actual3 = {
"third_url": nl3.GetNthURL(links_file, 3),
"third_desc": nl3.GetNthDescription(links_file, 3),
"oob_url": nl3.GetNthURL(links_file, 99),
}
cases.append({
"name": "nextlink_nth_item_lookup",
"summary": "Verify GetNthURL and GetNthDescription return 1-based items and empty for out-of-range.",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: nlcode.inc navigation bar rendering at first, middle, and last pages
nav_first = render_nav_bar(MockNextLink("asp_intro.asp"), links_file)
nav_mid = render_nav_bar(MockNextLink("asp_syntax.asp"), links_file)
nav_last = render_nav_bar(MockNextLink("asp_procedures.asp"), links_file)
expected4 = {
"first": "<a href='asp_syntax.asp'>Next Page</a>",
"middle": "<a href='asp_intro.asp'>Previous Page</a> <a href='asp_variables.asp'>Next Page</a>",
"last": "<a href='asp_variables.asp'>Previous Page</a>",
}
actual4 = {"first": nav_first, "middle": nav_mid, "last": nav_last}
cases.append({
"name": "nextlink_nlcode_inc_nav_render",
"summary": "Verify nlcode.inc navigation links suppress Previous on page 1 and Next on last page.",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
print(json.dumps({
"version": "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)",
"cases": cases,
}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
在最后一页时 GetNextURL 返回空字符串,会渲染出无效的空链接;应增加条件判断 If nl.GetListIndex("links.txt") < nl.GetListCount("links.txt") Then 再输出 Next Page。