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 =3D
Request.Url.AbsoluteUri.ToString.Split("/"c)
Dim Protocol As String =3D Parts(0)
Dim MoreParts() As String =3D Parts(2).Split("/")
Dim Server As String =3D MoreParts(0)
Dim ProtocolAndServer As String =3D Protocol & "//" & Server
VirtualPath =3D VirtualPath.Trim
If VirtualPath.Contains("~") Then
VirtualPath =3D VirtualPath.Replace("~",
..ApplicationPath)
End If
Dim FullURL As String =3D ProtocolAndServer & "/" &
VirtualPath.TrimStart("/")
Return FullURL.TrimEnd("/")
End With
End Function
Tommaso
Juan T. Llibre ha scritto:
[quoted text, click to view] > re:
> > but * we still have the problem * to transform the virtual path in a UR=
L=2E
>
> Do you mean this :
> > Response.Write(FullURLFromVirtualPath("~/App_Data"))
>
> If you do, sure, but you don't really need to execute anything in App_Dat=
a, do you ?
Names are just by fantasy ....
[quoted text, click to view] > 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 FullURLFromVi=
rtualPath,
> 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 =3D .ServerVariables("SERVER_PORT")
> > > If (Port Is Nothing OrElse Port =3D "80" OrElse Port =3D "=
443")
> > > Then
> > > Port =3D ""
> > > Else
> > > Port =3D ":" & Port
> > > End If
> > > Dim Protocol As String =3D
> > > .ServerVariables("SERVER_PORT_SECURE")
> > > If (Protocol Is Nothing OrElse Protocol =3D "0") Then
> > > Protocol =3D "http://"
> > > Else
> > > Protocol =3D "https://"
> > > End If
>
> ...you'd simply use :
>
> If Request.IsSecureConnection Then
> Dim Protocol As string =3D "https://"
> Else
> Dim Protocol As string =3D "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.Ur=
l=2EHost.
>
>
>
>
> 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=F1ol :
http://asp.net.do/foros/ > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> <tommaso.gastaldi@uniroma1.it> wrote in message
> news:1155904633.910556.85420@i3g2000cwc.googlegroups.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=F1ol :
http://asp.net.do/foros/ > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > <tommaso.gastaldi@uniroma1.it> wrote in message
> > news:1155901373.586580.201490@m73g2000cwd.googlegroups.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 y=
ou
> > > and hear your possible opinions, corrections, improvements, flaws, et=
c=2E
> > >
> > >
> > > Tom
> > >
> > > Function FullURLFromVirtualPath(ByVal VirtualPath As String) As
> > > String
> > >
> > > With HttpContext.Current.Request
> > >
re:
[quoted text, click to view] > but * we still have the problem * to transform the virtual path in a URL.
Do you mean this :
[quoted text, click to view] > 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:
[quoted text, click to view] > > 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 :
[quoted text, click to view] > > 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/ ===================================
[quoted text, click to view] <tommaso.gastaldi@uniroma1.it> wrote in message
news:1155904633.910556.85420@i3g2000cwc.googlegroups.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:
[quoted text, click to view] > 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/ > ===================================
> <tommaso.gastaldi@uniroma1.it> wrote in message
> news:1155901373.586580.201490@m73g2000cwd.googlegroups.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?
> >