ASP Content Linking

用 Content Linking 组件做上一页/下一页导航、目录列表。

Intermediate
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 页
上一页:第一章 概述 下一页:第三章 语法

方法一览

MethodReturns
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.

什么时候值得用

  • 线性教程 —— 章节有严格顺序,需要上一页/下一页
  • 不需要后台 —— 改导航只要改一个文本文件
  • 不适合 —— 树形结构、需要权限、条目上千(每次请求都读文件)

Friends & Partners