EfficionDave
EfficionDave

Reputation: 2746

Determine site's absolute, fully-qualified url in asp.net

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:

Upvotes: 8

Views: 8354

Answers (7)

EfficionDave
EfficionDave

Reputation: 2746

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();
}

Upvotes: 12

Scott Stafford
Scott Stafford

Reputation: 44776

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

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

Upvotes: 7

devio
devio

Reputation: 37205

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

Upvotes: 2

Dave Neeley
Dave Neeley

Reputation: 3635

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

Upvotes: 5

Omar Kooheji
Omar Kooheji

Reputation: 55760

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.

Upvotes: 0

Stephen Wrighton
Stephen Wrighton

Reputation: 37819

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)

Upvotes: 0

Orion Adrian
Orion Adrian

Reputation: 19543

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

Upvotes: 0

Related Questions