ASP Content Linking
用 Content Linking 组件做上一页/下一页导航、目录列表。
Key PointsContent Linking 读一个纯文本的链接清单,自动算出上一页、下一页、目录列表。
清单文件
Text
ch01.asp 第一章 概述
ch02.asp 第二章 安装
ch03.asp 第三章 语法
ch04.asp 第四章 进阶Each line is "page address + whitespace + description". The description may be omitted, but then navigation shows only the link. The file must be inside the site, with paths relative to the page using it.
上一页 / 下一页
ASP
<%
Dim nl, prev, next_
Set nl = Server.CreateObject("MSWC.NextLink")
Response.Write "共 " & nl.GetCount("/nav/contents.txt") & " 页<br>"
Response.Write "当前是第 " & nl.GetListIndex("/nav/contents.txt") & " 页<br>"
prev = nl.GetPreviousURL("/nav/contents.txt")
If prev <> "" Then
Response.Write "<a href=""" & prev & """>上一页:" & _
nl.GetNthDescription("/nav/contents.txt", _
nl.GetListIndex("/nav/contents.txt") - 1) & "</a> "
End If
next_ = nl.GetNextURL("/nav/contents.txt")
If next_ <> "" Then
Response.Write "<a href=""" & next_ & """>下一页:" & _
nl.GetNthDescription("/nav/contents.txt", _
nl.GetListIndex("/nav/contents.txt") + 1) & "</a>"
End If
Set nl = Nothing
%>Output
共 4 页
当前是第 2 页
上一页:第一章 概述 下一页:第三章 语法方法一览
| Method | Returns |
|---|---|
GetListCount | 条目数量 |
GetListIndex | 当前页在清单里的序号,不在清单里返回 0 |
GetNextURL | 最后一页返回空串 |
GetPreviousURL | 第一页返回空串 |
GetNthURL | 按序号取 |
GetNthDescription | 按序号取说明 |
GetNextDescription | |
GetPreviousDescription |
GetListIndex locates the current page by comparing the request URL with the list paths. The paths must match how pages are actually requested, or it always returns 0.
目录列表
ASP
<%
Dim i, n, url
n = nl.GetListCount("/nav/contents.txt")
Response.Write "<ol>"
For i = 1 To n
url = nl.GetNthURL("/nav/contents.txt", i)
Response.Write "<li><a href=""" & url & """>" & _
nl.GetNthDescription("/nav/contents.txt", i) & "</a></li>"
Next
Response.Write "</ol>"
%>Output
1. 第一章 概述
2. 第二章 安装
3. 第三章 语法
4. 第四章 进阶GetListIndex needs an exact URL match. Rewrites, query strings or different casing can all make it return 0 and the navigation disappears. Test with real URLs before shipping.什么时候值得用
- 线性教程 —— 章节有严格顺序,需要上一页/下一页
- 不需要后台 —— 改导航只要改一个文本文件
- 不适合 —— 树形结构、需要权限、条目上千(每次请求都读文件)
清单文件
Text
ch01.asp 第一章 概述
ch02.asp 第二章 安装
ch03.asp 第三章 语法
ch04.asp 第四章 进阶每行「页面地址 + 空白 + 说明」。说明可以省略,但省略后导航里就只剩链接文字。文件必须放在站点内,路径相对于使用它的页面。
上一页 / 下一页
ASP
<%
Dim nl, prev, next_
Set nl = Server.CreateObject("MSWC.NextLink")
Response.Write "共 " & nl.GetCount("/nav/contents.txt") & " 页<br>"
Response.Write "当前是第 " & nl.GetListIndex("/nav/contents.txt") & " 页<br>"
prev = nl.GetPreviousURL("/nav/contents.txt")
If prev <> "" Then
Response.Write "<a href=""" & prev & """>上一页:" & _
nl.GetNthDescription("/nav/contents.txt", _
nl.GetListIndex("/nav/contents.txt") - 1) & "</a> "
End If
next_ = nl.GetNextURL("/nav/contents.txt")
If next_ <> "" Then
Response.Write "<a href=""" & next_ & """>下一页:" & _
nl.GetNthDescription("/nav/contents.txt", _
nl.GetListIndex("/nav/contents.txt") + 1) & "</a>"
End If
Set nl = Nothing
%>Output
共 4 页
当前是第 2 页
上一页:第一章 概述 下一页:第三章 语法方法一览
| 方法 | 返回 |
|---|---|
| 总数 | 条目数量 |
| 当前位置 | 当前页在清单里的序号,不在清单里返回 0 |
| 下一页地址 | 最后一页返回空串 |
| 上一页地址 | 第一页返回空串 |
| 第 n 页地址 | 按序号取 |
| 第 n 页说明 | 按序号取 |
| 下一页说明 | |
| 上一页说明 |
GetListIndex 靠比对当前请求的 URL 与清单里的路径来判断位置。所以清单里的路径写法必须与实际访问路径一致,否则永远返回 0。
目录列表
ASP
<%
Dim i, n, url
n = nl.GetListCount("/nav/contents.txt")
Response.Write "<ol>"
For i = 1 To n
url = nl.GetNthURL("/nav/contents.txt", i)
Response.Write "<li><a href=""" & url & """>" & _
nl.GetNthDescription("/nav/contents.txt", i) & "</a></li>"
Next
Response.Write "</ol>"
%>Output
1. 第一章 概述
2. 第二章 安装
3. 第三章 语法
4. 第四章 进阶GetListIndex 依赖 URL 完全匹配。页面被重写、带查询串、或用不同大小写访问时都可能返回 0,导航就整块消失。上线前要在真实 URL 下测一遍。什么时候值得用
- 线性教程 —— 章节有严格顺序,需要上一页/下一页
- 不需要后台 —— 改导航只要改一个文本文件
- 不适合 —— 树形结构、需要权限、条目上千(每次请求都读文件)