经典ASP:查询字符串处理程序

时间:2010-08-12 18:46:05

标签: asp-classic query-string handler

我很久以前就已经这样做了,现在我找不到这个功能了。它应该不会太复杂,但我想知道在我再去之前是否有任何新闻......

拿这个:

www.example.com?query=whatever&page=1

现在想象一下我按下按钮到第2页,它将变为:

www.example.com?query=whatever&page=2

始终保持其余的查询字符串完好无损。现在第2页上的图片我按下按钮按日期排序,它应该变为:

www.example.com?query=whatever&page=1&order=date

问题是,在ASP代码上进行排序,我不想处理所有其他的查询字符串。所以我需要一个函数来为我处理它,并能够做类似下面的例子:

<a href="?<%= add_querystring(qs, "order", "date") %>">date</a>
<a href="?<%= set_querystring(qs, "page", page + 1) %>">next page</a>
<a href="?<%= add_querystring(del_querystring(qs, "page"), "allpages") %>">all pages</a>

如果我仍然找不到一个现成的解决方案,这只是我要做的事情的初步想法......再次,只是想知道是否有任何新的东西可以处理所有这些,我甚至没有想象中的。

1 个答案:

答案 0 :(得分:3)

如果这符合任何人的利益,这就是我昨天提出的令人困惑的代码:

'Build a string QueryString from the array Request
function bdl_qs (req_qs)
    dim result, qa, item
    result = empty
    qa = "?"
    if isnull(req_qs) or isempty(req_qs) then req_qs = Request.QueryString
    for each item in req_qs
        result = result & qa & item
        result = result & "=" & req_qs(item)
        qa = "&"
    next
    bdl_qs = result
end function

'Build a string QueryString ontop of the supplied one, adding the query and / or value(s) to it
function add_qs (qs, q, s)
    dim result
    result = qs
    if left(result, 1) = "?" then
        result = result & "&" & q
    else
        result = "?" & q
    end if
    if not isnull(s) and not isempty(s) then 
        result = result & "=" & s
    end if  
    add_qs = result
end function

'Build a string QueryString ontop of the supplied one, removing the selected query and / or values
function del_qs (qs, q)
    dim result, item
    result = qs
    if left(qs, 1) = "?" then
        dim rqs, qa
        rqs = result
        result = "?"
        rqs = right(rqs, len(rqs)-1) 'remove the "?"
        rqs = Split(rqs, "&") 'separate the queries
        qa = ""
        for each item in rqs
            dim rq
            rq = Split(item, "=") 'separate the query to analyze the name only
            if rq(0) <> q then 'good for rebuilding
                result = result & qa & item
                qa = "&"
            end if
        next
    end if
    del_qs = result
end function

'Build a string QueryString ontop of the supplied one, setting the query to the value
function set_qs (qs, q, s)
    set_qs = add_qs(del_qs(qs, q), q, s)
end function