13

I may not be the first person to ask this question but I can't find what I was looking for after looking around. I want to get base URL from the URL. I have tried

    HttpContext.Current.Request.Url.AbsoluteUri

It would return me the full URL

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

I would just need until here (e.g.)

    http://localhost:59112/

Thanks for your help.

Laurence
  • 7,633
  • 21
  • 78
  • 129

5 Answers5

19

With a bit more research .. I got what I want from a forum .. this is brilliant solution ..

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

Thanks

Laurence
  • 7,633
  • 21
  • 78
  • 129
5

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

HttpContext.Current.Request.Url.Authority

Return: "localhost:59112"

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

Return: "http://localhost:59112"

Diogo Rodrigues
  • 1,312
  • 15
  • 15
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
RainerJ
  • 71
  • 2
  • 10
0

In .Net 4 and later you can use -

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"
Adriano Martins
  • 1,788
  • 1
  • 19
  • 23
John Butler
  • 121
  • 6
-1

HttpContext.Current.Request.Url.Host

J0e3gan
  • 8,740
  • 10
  • 53
  • 80