T
tommaso.gastaldi
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?
I was in the need to find a sort of "definitive"

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?