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? 4 1481
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?
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?
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?
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?
>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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.
...
|
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...
|
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.
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: marss |
last post by:
Server.MapPath("~/page.aspx") returns the physical file path based on
the specified virtual path. Is there any reverse method to get the
virtual path based on the physical path (both pathes belong...
|
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,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |