FULL URL from VIRTUAL path

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?
 
T

tommaso.gastaldi

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:
 
J

Juan T. Llibre

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:
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 :

....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.






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:
 
T

tommaso.gastaldi

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:

Do you mean this :

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:
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 :

...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.






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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top