如何使用ASP而不是ASP.net获取根URL

时间:2011-12-22 06:41:36

标签: asp-classic

如何使用ASP而不是ASP.net获取根URL? 我发现了这个问题( How do I get the site root URL?

但它与ASP.net有关。

=====================================

阿巴斯的回答为我提供了

父网站根网址

但未向我提供子网站根网址

=====================================

3 个答案:

答案 0 :(得分:13)

Classic ASP有一个Request.ServerVariables集合,其中包含所有服务器和环境详细信息。以下是.NET代码示例的经典ASP版本:

function getSiteRootUrl()
    dim siteRootUrl, protocol, hostname, port

    if Request.ServerVariables("HTTPS") = "off" then
        protocol = "http"
    else
        protocol = "https"
    end if
    siteRootUrl = protocol & "://"

    hostname = Request.ServerVariables("HTTP_HOST")
    siteRootUrl = siteRootUrl & hostname        

    port = Request.ServerVariables("SERVER_PORT")
    if port <> 80 and port <> 443 then
        siteRootUrl = siteRootUrl & ":" & port
    end if

    getSiteRootUrl = siteRootUrl
end function

答案 1 :(得分:1)

这可以让你得到你想要的东西。

getSiteURL()

Function getSiteURL()

    dim port
    dim https 
    dim domainname
    dim filename
    dim querystring
    dim fullpath
    dim url

    port = "http" 
    https = lcase(request.ServerVariables("HTTPS")) 
    if https <> "off" then port = "https" 
    domainname = Request.ServerVariables("SERVER_NAME") 
    filename = Request.ServerVariables("SCRIPT_NAME") 
    querystring = Request.ServerVariables("QUERY_STRING") 
    fullpath = port & "://" & domainname & Request.ServerVariables("SCRIPT_NAME")
    filename = right(fullpath, InStr(StrReverse(fullpath), StrReverse("/")))

    url = Replace(fullpath, filename, "/")

    response.write url & "<br>" 
end Function 

答案 2 :(得分:0)

尽管这是一个非常老的问题,但我怀疑Hoque要求的是页面之前的所有内容。例如,如果URL为http://www.example.com/subsite/default.asp?a=1&b=2,则预期的返回值为http://www.example.com/subsite

Function GetSitePath()
    Dim address, port, url
    address = "http://"
    If LCase(Request.ServerVariables("HTTPS")) = "off" Then address = "https://"
    address = address & Request.ServerVariables("SERVER_NAME")
    port = Request.ServerVariables("SERVER_PORT")
    If port <> 80 And port <> 443 Then address = address & ":" & port
    url = Request.ServerVariables("URL")
    address = address & Left(url, InstrRev(url, "/"))
    GetSitePath = address
End Function

请注意,为了防止出现错误的网址,我在此处未进行任何错误检查,但是如果这会导致错误,我会怀疑还有更多需要解决的问题!