• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Getting the base URL of an application


How do I get the domain name of which my application runs on? For example, in my application test environment, visual studio 2008 gives me a random url, such as:


I want this portion of the url: http://localhost:1141
  • 2
1 Solution
I do it like this:
Dim HTTP_HOST As String = Request.Url.GetLeftPart(UriPartial.Authority) & Page.ResolveUrl("~/")
I think this one:

      Session("absURL") = Request.Url.AbsoluteUri

alfardanAuthor Commented:
I think your code is the one I'm looking for, howeverm if I do place this code in an aspx.vb file then i works fine, however, if I place it in a class that is located in the App_Code folder (as per Visual Studio 2005+ requirement) then it gives me an error for using the (Page.ResolveUrl("~/")) , it says: Reference to a non-shared member requires an object reference.
What's that mean?
All I did is that I placed your code in a function that is in my class that is located in App)Code folder. See the declaration of this function in the code section of this post.

Public Function GetMyDomain() As String
Return Current.Request.Url.GetLeftPart(UriPartial.Authority) & Page.ResolveUrl("~/")
End Function

Open in new window

alfardanAuthor Commented:
I used your code, but it didn't give me exactly what I wanted, but it helped me in getting what I wanted. This is the solution I needed exactly:

Public Function GetMyDomainLink() As String

        Dim DomainLink As String = Current.Request.Url.AbsoluteUri
        Dim Spliter() As String = Split(DomainLink, "://")
        DomainLink = Mid(Spliter(1), 1, Spliter(1).IndexOf("/"))
        DomainLink = Spliter(0) + "://" + DomainLink

        Return DomainLink

    End Function
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now