8

How can I consistently get the absolute, fully-qualified root or base url of the site regardless of whether the site is in a virtual directory and regardless of where my code is in the directory structure? I've tried every variable and function I can think of and haven't found a good way.

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


Here's some of the things I've tried and the result. The only one that includes the whole piece 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: /DNN441/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: /DNN441/Default.aspx?TabId=56&ctl=Details&mid=374&ItemID=1

7 Answers 7

12

In reading through the answer provided in Rick Strahl's Blog I found what I really needed was quite simple. First you need to determine the relative path (which for me was the easy part), and pass that into 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

C#

public static string GetFullyQualifiedURL(string s) {
    Uri Result = new Uri(HttpContext.Current.Request.Url, s);
    return Result.ToString();
}
1
  • 1
    I think that only works if your current request is at the root already. Otherwise it's relative to the current requests' dir, not the server application path root. I have had success with: Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath Aug 18, 2011 at 18:45
7

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

Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
1
  • Sometimes I do find this to be the preferred option. Not enough to change it to the accepted answer yet, but perhaps in time. Feb 18, 2012 at 23:22
5

There is some excellent discussion and ideas on Rick Strahl's blog

EDIT: I should add that the ideas work with or without a valid HttpContext.

EDIT2: Here's the specific comment / code on that post that answers the question

1
  • The final post on the page does seem to be the answer I need. I haven't "Accepted" this answer though cause I hate for people to have to read through everything there in order to get to the final answer... Not sure if I should just repost the last post here or what the best resolution is. Sep 24, 2008 at 18:46
2

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);
0

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

1
  • This will be for a DotNetNuke module and most DotNetNuke sites do not have this setting. Sep 23, 2008 at 16:42
0

Are you talking about for use as links?

if so, then doing this <a href='/'>goes to root</a> will take you to the default file of the web root.

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

1
  • No, these would both be relative links. I need the absolute, fully qualified root. I have a variety of cases where I need this, the specific one right now is so I can pass the return and cancel urls to Paypal. Sep 23, 2008 at 16:44
0

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

It occurs to me that as far as your machine is concerned it's browser is requesting from localhost.

I could be wrong though but I think request is relative to the browser and not the webserver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.