473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3203
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PHaroZ | last post by:
Hi, I want to retrieve the complete full path to the directory of my current page but i don't find how to do that. For example i want : D:\myWebSite\firstDotNetWebApp\dir1\ I tried...
4
by: Tee | last post by:
Hi, Can anyone tell me how to get the path of IIS root folder from coding? Eg: C:\Inetpub\wwwroot. I would like to detect it via coding as not everyone set it at the default folder. ...
14
by: Lorenzo | last post by:
Hello, I have a web application with a virtual directory in it. With 'virtual directory' i mean a folder whose physical path is different from the physical path of the application, but in which...
2
by: ABC | last post by:
How to return the full virtual path if I pass the url as "~/default.aspx"? I expected this should result as http://aaa.bbb.com/default.aspx.
4
by: Dots | last post by:
I have a class library with a method called getpath(). I want to be able to get the full path of a folder and write some files to the (my_files_dir) folder. A console application will use this...
4
by: tommaso.gastaldi | last post by:
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...
8
by: JJ | last post by:
I'm confused about paths. I have a functionn that uses the mappath method, which I think requires a virtual path (is that the same as a relative path?). But this doesn't always work as the...
4
by: =?Utf-8?B?SmVmZiBCZWVt?= | last post by:
Best way I can think to describe this is through an example. I have a virtual directory, let's call it "MyVirtualDirectory" that maps to \\MyServer\Shared. I have a path that is...
4
by: tshad | last post by:
I have a site www.stf.com and a site www.stfstage.com (where I do all my testing). The problem is that www.stfstage.com is only internal and I need to get access from the outside (without...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.