469,610 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

FULL URL from VIRTUAL path

Hi friends

I was in the need to find a sort of "definitive" :) solution to
transform a
virtual path such as "~/MyDir/MyFile into a full web address. In
particular I needed
it * within web services *.

I would like to be informed that the framework provides a function to
do that, but for now I am missing it.

After some googling around, I have assembled (not invented) the
following
function, which would *seem* to work. I would like to share it with you
and hear your possible opinions, corrections, improvements, flaws, etc.
Tom

Function FullURLFromVirtualPath(ByVal VirtualPath As String) As
String

With HttpContext.Current.Request

Dim Port As String = .ServerVariables("SERVER_PORT")
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
..ServerVariables("SERVER_PORT_SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
..ServerVariables("SERVER_NAME") & Port

Dim LocalPath As String
If VirtualPath.TrimStart(" ").StartsWith("~") Then
LocalPath = (.ApplicationPath &
VirtualPath.Substring(1))
Else
LocalPath = VirtualPath.Replace("//", "/") 'is this
needed at all??
End If

Return ServerUrl & LocalPath

End With

End Function

I am also in doubt if, to be precise, the 443 should be considered
*only* for secure
conns. So probably better to place that if within the protocol if.
Opinions?

Aug 18 '06 #1
4 3017
Both Request.Url.ToString()
and
Request.Url.AbsoluteUri.ToString()

will provide a full http, or https, web address from a virtual path.

See a sample at http://asp.net.do/test/pathinfo.aspx

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<to**************@uniroma1.itwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi friends

I was in the need to find a sort of "definitive" :) solution to
transform a
virtual path such as "~/MyDir/MyFile into a full web address. In
particular I needed it * within web services *.

I would like to be informed that the framework provides a function to
do that, but for now I am missing it.

After some googling around, I have assembled (not invented) the
following
function, which would *seem* to work. I would like to share it with you
and hear your possible opinions, corrections, improvements, flaws, etc.
Tom

Function FullURLFromVirtualPath(ByVal VirtualPath As String) As
String

With HttpContext.Current.Request

Dim Port As String = .ServerVariables("SERVER_PORT")
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariables("SERVER_PORT_SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
.ServerVariables("SERVER_NAME") & Port

Dim LocalPath As String
If VirtualPath.TrimStart(" ").StartsWith("~") Then
LocalPath = (.ApplicationPath &
VirtualPath.Substring(1))
Else
LocalPath = VirtualPath.Replace("//", "/") 'is this
needed at all??
End If

Return ServerUrl & LocalPath

End With

End Function

I am also in doubt if, to be precise, the 443 should be considered
*only* for secure
conns. So probably better to place that if within the protocol if.
Opinions?

Aug 18 '06 #2
Thanks Juan,

this seems to be a step ahed, as it could simplify the retrieval of the
first part of the URL, but * we still have the problem * to transform
the virtual path in a URL. Any idea?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Response.Write(Request.Url.ToString())
Response.Write("<br>")

Response.Write(Request.Url.AbsoluteUri.ToString)
Response.Write("<br>")

Response.Write(FullURLFromVirtualPath("~/App_Data"))
Response.Write("<br>")

End Sub
Returns (for example):

http://localhost:4139/WebSite1/Default.aspx
http://localhost:4139/WebSite1/Default.aspx
http://localhost:4139/WebSite1/App_Data
Juan T. Llibre ha scritto:
Both Request.Url.ToString()
and
Request.Url.AbsoluteUri.ToString()

will provide a full http, or https, web address from a virtual path.

See a sample at http://asp.net.do/test/pathinfo.aspx

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<to**************@uniroma1.itwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi friends

I was in the need to find a sort of "definitive" :) solution to
transform a
virtual path such as "~/MyDir/MyFile into a full web address. In
particular I needed it * within web services *.

I would like to be informed that the framework provides a function to
do that, but for now I am missing it.

After some googling around, I have assembled (not invented) the
following
function, which would *seem* to work. I would like to share it with you
and hear your possible opinions, corrections, improvements, flaws, etc.
Tom

Function FullURLFromVirtualPath(ByVal VirtualPath As String) As
String

With HttpContext.Current.Request

Dim Port As String = .ServerVariables("SERVER_PORT")
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariables("SERVER_PORT_SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
.ServerVariables("SERVER_NAME") & Port

Dim LocalPath As String
If VirtualPath.TrimStart(" ").StartsWith("~") Then
LocalPath = (.ApplicationPath &
VirtualPath.Substring(1))
Else
LocalPath = VirtualPath.Replace("//", "/") 'is this
needed at all??
End If

Return ServerUrl & LocalPath

End With

End Function

I am also in doubt if, to be precise, the 443 should be considered
*only* for secure
conns. So probably better to place that if within the protocol if.
Opinions?
Aug 18 '06 #3
re:
but * we still have the problem * to transform the virtual path in a URL.
Do you mean this :
Response.Write(FullURLFromVirtualPath("~/App_Data"))
If you do, sure, but you don't really need to execute anything in App_Data, do you ?
You don't really need that because virtual URLs are just as good as fully-qualified URLs.

re:
I am also in doubt if, to be precise, the 443 should be considered
*only* for secure conns.
You'll find it a bit easier, if you want to continue to use FullURLFromVirtualPath,
to use Request.IsSecureConnection to determine if http or https needs to be used.

That way, instead of the needlessly complex :
Dim Port As String = .ServerVariables("SERVER_PORT")
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariables("SERVER_PORT_SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
....you'd simply use :

If Request.IsSecureConnection Then
Dim Protocol As string = "https://"
Else
Dim Protocol As string = "http://"
End If

Also, instead of loading all of the ServerVariables collection,
[ when you get ("SERVER_NAME") and ("SERVER_PORT") ]
you might find it more economical to load the server name with Request.Url.Host.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<to**************@uniroma1.itwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...

Thanks Juan,

this seems to be a step ahed, as it could simplify the retrieval of the
first part of the URL, but * we still have the problem * to transform
the virtual path in a URL. Any idea?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Response.Write(Request.Url.ToString())
Response.Write("<br>")

Response.Write(Request.Url.AbsoluteUri.ToString)
Response.Write("<br>")

Response.Write(FullURLFromVirtualPath("~/App_Data"))
Response.Write("<br>")

End Sub
Returns (for example):

http://localhost:4139/WebSite1/Default.aspx
http://localhost:4139/WebSite1/Default.aspx
http://localhost:4139/WebSite1/App_Data
Juan T. Llibre ha scritto:
Both Request.Url.ToString()
and
Request.Url.AbsoluteUri.ToString()

will provide a full http, or https, web address from a virtual path.

See a sample at http://asp.net.do/test/pathinfo.aspx

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<to**************@uniroma1.itwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi friends

I was in the need to find a sort of "definitive" :) solution to
transform a
virtual path such as "~/MyDir/MyFile into a full web address. In
particular I needed it * within web services *.

I would like to be informed that the framework provides a function to
do that, but for now I am missing it.

After some googling around, I have assembled (not invented) the
following
function, which would *seem* to work. I would like to share it with you
and hear your possible opinions, corrections, improvements, flaws, etc.
Tom

Function FullURLFromVirtualPath(ByVal VirtualPath As String) As
String

With HttpContext.Current.Request

Dim Port As String = .ServerVariables("SERVER_PORT")
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariables("SERVER_PORT_SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
.ServerVariables("SERVER_NAME") & Port

Dim LocalPath As String
If VirtualPath.TrimStart(" ").StartsWith("~") Then
LocalPath = (.ApplicationPath &
VirtualPath.Substring(1))
Else
LocalPath = VirtualPath.Replace("//", "/") 'is this
needed at all??
End If

Return ServerUrl & LocalPath

End With

End Function

I am also in doubt if, to be precise, the 443 should be considered
*only* for secure
conns. So probably better to place that if within the protocol if.
Opinions?

Aug 18 '06 #4
Hi Juan,

I am not sure what you mean by "virtual URLs are just as good as
fully-qualified URLs"
I think you are assuming to work only within the application.

My perspective is different. I want, given a file which is on a given
virtual path,
to be able to tell an *external user* what is the URL he must enter in
the browser to reach that same object. Is it clear? For instance. If
she enter "~/" he does not go anywhere.

In any case, see it as an abstract problem of conversion of a virtual
path to a fqu (no care about permissions or whatever).

Based on you observation I have rewritten the function as follows. Here
some examples of output:

http://localhost:4139/WebSite1/App_Data
http://localhost:4139/TecnicalPreview
http://localhost:4139/WebSite1/App_Data
http://localhost:4139
http://localhost:4139/WebSite1
http://localhost:4139/WebSite1/Images/Hi.jpg
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Response.Write(FullURLFromVirtualPath("//~/App_Data"))
Response.Write("<br>")

Response.Write(FullURLFromVirtualPath("/TechnicalPreview/"))
Response.Write("<br>")

Response.Write(FullURLFromVirtualPath("~/App_Data"))
Response.Write("<br>")

Response.Write(FullURLFromVirtualPath("// "))
Response.Write("<br>")

Response.Write(FullURLFromVirtualPath("//~"))
Response.Write("<br>")

Response.Write(FullURLFromVirtualPath("~/Images/Hi.jpg"))
Response.Write("<br>")

End Sub
'Based ob Juan observation:

Function FullURLFromVirtualPath(ByVal VirtualPath As String) As
String

With HttpContext.Current.Request

Dim Parts() As String =
Request.Url.AbsoluteUri.ToString.Split("/"c)
Dim Protocol As String = Parts(0)

Dim MoreParts() As String = Parts(2).Split("/")
Dim Server As String = MoreParts(0)

Dim ProtocolAndServer As String = Protocol & "//" & Server

VirtualPath = VirtualPath.Trim

If VirtualPath.Contains("~") Then
VirtualPath = VirtualPath.Replace("~",
..ApplicationPath)
End If

Dim FullURL As String = ProtocolAndServer & "/" &
VirtualPath.TrimStart("/")

Return FullURL.TrimEnd("/")

End With

End Function
Tommaso

Juan T. Llibre ha scritto:
re:
but * we still have the problem * to transform the virtual path in a URL.

Do you mean this :
Response.Write(FullURLFromVirtualPath("~/App_Data"))

If you do, sure, but you don't really need to execute anything in App_Data, do you ?

Names are just by fantasy ....

You don't really need that because virtual URLs are just as good as fully-qualified URLs.

re:
I am also in doubt if, to be precise, the 443 should be considered
*only* for secure conns.

You'll find it a bit easier, if you want to continue to use FullURLFromVirtualPath,
to use Request.IsSecureConnection to determine if http or https needs to be used.

That way, instead of the needlessly complex :
Dim Port As String = .ServerVariables("SERVER_PORT")
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariables("SERVER_PORT_SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If

...you'd simply use :

If Request.IsSecureConnection Then
Dim Protocol As string = "https://"
Else
Dim Protocol As string = "http://"
End If

Also, instead of loading all of the ServerVariables collection,
[ when you get ("SERVER_NAME") and ("SERVER_PORT") ]
you might find it more economical to load the server name with Request.Url.Host.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<to**************@uniroma1.itwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...

Thanks Juan,

this seems to be a step ahed, as it could simplify the retrieval of the
first part of the URL, but * we still have the problem * to transform
the virtual path in a URL. Any idea?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Response.Write(Request.Url.ToString())
Response.Write("<br>")

Response.Write(Request.Url.AbsoluteUri.ToString)
Response.Write("<br>")

Response.Write(FullURLFromVirtualPath("~/App_Data"))
Response.Write("<br>")

End Sub
Returns (for example):

http://localhost:4139/WebSite1/Default.aspx
http://localhost:4139/WebSite1/Default.aspx
http://localhost:4139/WebSite1/App_Data
Juan T. Llibre ha scritto:
Both Request.Url.ToString()
and
Request.Url.AbsoluteUri.ToString()

will provide a full http, or https, web address from a virtual path.

See a sample at http://asp.net.do/test/pathinfo.aspx

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<to**************@uniroma1.itwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi friends
>
I was in the need to find a sort of "definitive" :) solution to
transform a
virtual path such as "~/MyDir/MyFile into a full web address. In
particular I needed it * within web services *.
>
I would like to be informed that the framework provides a function to
do that, but for now I am missing it.
>
After some googling around, I have assembled (not invented) the
following
function, which would *seem* to work. I would like to share it with you
and hear your possible opinions, corrections, improvements, flaws, etc.
>
>
Tom
>
Function FullURLFromVirtualPath(ByVal VirtualPath As String) As
String
>
With HttpContext.Current.Request
>
Dim Port As String = .ServerVariables("SERVER_PORT")
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariables("SERVER_PORT_SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
.ServerVariables("SERVER_NAME") & Port
>
Dim LocalPath As String
If VirtualPath.TrimStart(" ").StartsWith("~") Then
LocalPath = (.ApplicationPath &
VirtualPath.Substring(1))
Else
LocalPath = VirtualPath.Replace("//", "/") 'is this
needed at all??
End If
>
Return ServerUrl & LocalPath
>
End With
>
End Function
>
I am also in doubt if, to be precise, the 443 should be considered
*only* for secure
conns. So probably better to place that if within the protocol if.
Opinions?
>
Aug 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Lorenzo | last post: by
4 posts views Thread by tommaso.gastaldi | last post: by
8 posts views Thread by JJ | last post: by
4 posts views Thread by =?Utf-8?B?SmVmZiBCZWVt?= | last post: by
4 posts views Thread by tshad | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.