Define an absolute, fully qualified website URL in asp.net

How can I consistently get the absolute, fully qualified root or base URL of a site, regardless of whether the site is in a virtual directory and no matter where my code is in the directory structure? I tried every variable and function that I can think of, and did not find a good way.

I want to get the url of the current site, i.e. http://www.example.com or if it is a virtual directory, http://www.example.com/DNN/


Here are some of the things I've tried and the result. The only one that includes the entire fragment that I want ( http: // localhost: 4471 / DNN441 ) is Request.URI.AbsoluteURI:

  • Request.PhysicalPath: C: \ WebSites \ DNN441 \ Default.aspx
  • Request.ApplicationPath: / DNN441
  • Request.PhysicalApplicationPath: C: \ WebSites \ DNN441 \
  • MapPath: C: \ WebSites \ DNN441 \ DesktopModules \ Articles \ Templates \ Default.aspx
  • RawURL: / DNN 441 / ModuleTesting / Articles / tabid / 56 / ctl / Details / mid / 374 / ItemID / 1 / Default.aspx
  • Request.Url.AbsoluteUri: http: // localhost: 4471 / DNN441 / Default.aspx
  • Request.Url.AbsolutePath: /DNN441/Default.aspx
  • Request.Url.LocalPath: /DNN441/Default.aspx Request.Url.Host: localhost
  • Request.Url.PathAndQuery: / DNN 441 / Default.aspx? TabId = 56 & ctl = Details & mid = 374 & ItemID = 1
+6
url uri
source share
7 answers

After reading the answer provided on the Rick Strahl blog, I found what I really needed was pretty simple. First you need to determine the relative path (which for me was the easy part) and pass this to the function defined below:

Vb.net

Public Shared Function GetFullyQualifiedURL(ByVal s as string) As String Dim Result as URI = New URI(HttpContext.Current.Request.Url, s) Return Result.ToString End Function 

FROM#

 public static string GetFullyQualifiedURL(string s) { Uri Result = new Uri(HttpContext.Current.Request.Url, s); return Result.ToString(); } 
+12
source share

The accepted answer assumes that the current request is already on the server / virtual root. Try the following:

 Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath 
+7
source share

There are some great discussions and ideas on Rick Stragg 's Blog.

EDIT: I have to add that ideas work with or without a valid HttpContext.

EDIT2: Here is a specific comment / code on this post that answers the question

+5
source share

Found this code here :

 string appPath = null; appPath = string.Format("{0}://{1}{2}{3}", Request.Url.Scheme, Request.Url.Host, Request.Url.Port == 80 ? string.Empty : ":" + Request.Url.Port, Request.ApplicationPath); 
+2
source share

Have you tried AppSettings.RootUrl, which is usually configured in the web.config file?

0
source share

Do you speak for use as links?

if so, then executing this <a href='/'>goes to root</a> will lead you to the default file for the web root.

Now, for the client side, passing "~ /" to the Control :: ResolveUrl method will provide you with what you are looking for. ( http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx )

0
source share

I have no way to verify this at the moment, but have you tried "Request.Url.AbsoluteUri" from another computer?

It occurs to me that in relation to your machine, the browser requests a local host.

I could be wrong, but, I think, the request relates to the browser, and not to the web server.

0
source share

All Articles