如何获取网站VB.NET的基本URL

时间:2012-09-24 15:50:04

标签: url hostname domain-name base-url

我可能不是第一个提出这个问题的人,但在环顾四周之后找不到我想要的东西。 我想从URL获取基本URL。我试过了

    HttpContext.Current.Request.Url.AbsoluteUri

它会返回完整的网址

    http://localhost:59112/Resources/VideoPlayer.aspx?ID=resources1.mp4

我只需要到这里(例如)

    http://localhost:59112/

感谢您的帮助。

5 个答案:

答案 0 :(得分:14)

通过更多研究......我从论坛得到了我想要的东西......这是很棒的解决方案..

    Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath

由于

答案 1 :(得分:2)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim address As Uri = New Uri("http://stackoverflow.com/questions/12568530/how-to-get-the-base-url-of-the-website-vb-net")
    MsgBox("http://" & address.Host)
End Sub

答案 2 :(得分:1)

Url ex:http://localhost:59112/Resources/VideoPlayer.aspx?ID=resources1.mp4

HttpContext.Current.Request.Url.Authority

返回:" localhost:59112"

HttpContext.Current.Request.Url.Scheme & "://" & HttpContext.Current.Request.Url.Authority

返回:" http://localhost:59112"

答案 3 :(得分:0)

HttpContext.Current.Request.Url.Host

答案 4 :(得分:0)

在.Net 4及更高版本中,您可以使用 -

Dim Url as String = Request.Url.OriginalString
Dim Domain as String = Url.Replace(Request.PathAndQuery, "")

Output -
Url = "http://localhost:9898/content/page.aspx
Domain = "http://localhost:9898"