How to get full url of virtual root


Ad

Advertisements

G

Guest

If you just want a client valid reference:
Dim root As String = Me.ResolveUrl("~")

If you actually want the whole URL to the root, you could build a function,
note it's untested but I can't think of any senarios where it wouldn't work.

Public Function AppRoot() As String
Dim appRelativeRoot As String = Me.ResolveUrl("~")
Dim originalUrl As String = Me.Request.Url.ToString
Dim pos As Integer = originalUrl.IndexOf(appRelativeRoot)
Dim path As String = appRelativeRoot
If pos > 0 Then
path = originalUrl.Substring(0, pos + appRelativeRoot.Length)
End If
Return path
End Function
 
J

Juan T. Llibre

Hi, Larry.

re:
If you actually want the whole URL to the root, you could build a function

Why go through those hoops, if Request.Url.ToString gets the whole URL ?
 
Ad

Advertisements

T

Thomas Jespersen

Hi Juan

Request.ApplicationPath! Why didn't I see that. I was going nots. Thanks.

Here is the final method, which also takes the port number and protocol
(http/https) into account:

private string FullApplicationPath()
{
return Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath,
string.Empty) + Request.ApplicationPath;
}

All the other methods simply didn't work, because they either removed the
Virtual directory or assumed that the everything else was on the same level
as the Request.

/Thomas
 
J

Juan T. Llibre

re:
Why are assuming that it's http?

Because this is an ASP.NET newsgroup ?
*All* questions here are supposed to be related to ASP.NET.

And, of course, ASP.NET works over http.
 
Ad

Advertisements

M

Michael Mooney

Juan T. Llibre said:
re:

Because this is an ASP.NET newsgroup ?
*All* questions here are supposed to be related to ASP.NET.

And, of course, ASP.NET works over http.

And, of course, ASP.NET also works over https. It can also include a port
number.

Hardcoding it to always be http is just a bad idea.
 
Ad

Advertisements

Joined
May 23, 2006
Messages
2
Reaction score
0
Private _RootUrl As String = Nothing
Public ReadOnly Property RootUrl() As String
Get
If String.IsNullOrEmpty(_RootUrl) Then
Dim builder As New System.Text.StringBuilder
builder.Append(Request.ServerVariables("SERVER_PROTOCOL").Split("/")(0).ToLower())
builder.Append("://")
builder.Append(Request.Url.Host)
If Not String.IsNullOrEmpty(Request.ServerVariables("SERVER_PORT")) AndAlso _
Not Request.ServerVariables("SERVER_PORT") = "80" Then
builder.Append(":")
builder.Append(Request.ServerVariables("SERVER_PORT"))
End If
If Not String.IsNullOrEmpty(Request.ApplicationPath()) Then
If Not Request.ApplicationPath().StartsWith("/") Then
builder.Append("/")
End If
builder.Append(Request.ApplicationPath())
End If
If Not builder.ToString.Trim.EndsWith("/") Then
builder.Append("/")
End If
_RootUrl = builder.ToString
End If
Return _RootUrl
End Get
End Property

I'd be interested to know if this works with URL rewriting.
 

Top