首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何使用ASP而不是ASP.net获取根url

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

Stack Overflow用户
提问于 2011-12-22 14:41:36
回答 3查看 11.4K关注 0票数 6

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

但它与ASP.net有关。

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

阿巴斯的回答为我提供了

父站点根url

但不向我提供子站点根url。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-12-22 15:20:55

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

代码语言:javascript
运行
复制
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
票数 13
EN

Stack Overflow用户

发布于 2011-12-23 00:06:23

这应该会让你得到你想要的。

代码语言:javascript
运行
复制
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 
票数 1
EN

Stack Overflow用户

发布于 2019-11-26 19:18:37

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

代码语言:javascript
运行
复制
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

请注意,我在这里没有包含错误检查,以防止错误的URL,但如果这导致了错误,我会怀疑有一些更令人绝望的事情正在发生!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8600266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档