473,776 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 FullURLFromVirt ualPath(ByVal VirtualPath As String) As
String

With HttpContext.Cur rent.Request

Dim Port As String = .ServerVariable s("SERVER_PORT" )
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
..ServerVariabl es("SERVER_PORT _SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
..ServerVariabl es("SERVER_NAME ") & Port

Dim LocalPath As String
If VirtualPath.Tri mStart(" ").StartsWith(" ~") Then
LocalPath = (.ApplicationPa th &
VirtualPath.Sub string(1))
Else
LocalPath = VirtualPath.Rep lace("//", "/") '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 3238
Both Request.Url.ToS tring()
and
Request.Url.Abs oluteUri.ToStri ng()

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.itw rote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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 FullURLFromVirt ualPath(ByVal VirtualPath As String) As
String

With HttpContext.Cur rent.Request

Dim Port As String = .ServerVariable s("SERVER_PORT" )
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariable s("SERVER_PORT_ SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
.ServerVariable s("SERVER_NAME" ) & Port

Dim LocalPath As String
If VirtualPath.Tri mStart(" ").StartsWith(" ~") Then
LocalPath = (.ApplicationPa th &
VirtualPath.Sub string(1))
Else
LocalPath = VirtualPath.Rep lace("//", "/") '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.EventArg s) Handles Me.Load

Response.Write( Request.Url.ToS tring())
Response.Write( "<br>")

Response.Write( Request.Url.Abs oluteUri.ToStri ng)
Response.Write( "<br>")

Response.Write( FullURLFromVirt ualPath("~/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.ToS tring()
and
Request.Url.Abs oluteUri.ToStri ng()

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.itw rote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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 FullURLFromVirt ualPath(ByVal VirtualPath As String) As
String

With HttpContext.Cur rent.Request

Dim Port As String = .ServerVariable s("SERVER_PORT" )
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariable s("SERVER_PORT_ SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
.ServerVariable s("SERVER_NAME" ) & Port

Dim LocalPath As String
If VirtualPath.Tri mStart(" ").StartsWith(" ~") Then
LocalPath = (.ApplicationPa th &
VirtualPath.Sub string(1))
Else
LocalPath = VirtualPath.Rep lace("//", "/") '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( FullURLFromVirt ualPath("~/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 FullURLFromVirt ualPath,
to use Request.IsSecur eConnection to determine if http or https needs to be used.

That way, instead of the needlessly complex :
Dim Port As String = .ServerVariable s("SERVER_PORT" )
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariable s("SERVER_PORT_ SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
....you'd simply use :

If Request.IsSecur eConnection 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_NAM E") and ("SERVER_POR T") ]
you might find it more economical to load the server name with Request.Url.Hos t.


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.itw rote in message
news:11******** *************@i 3g2000cwc.googl egroups.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.EventArg s) Handles Me.Load

Response.Write( Request.Url.ToS tring())
Response.Write( "<br>")

Response.Write( Request.Url.Abs oluteUri.ToStri ng)
Response.Write( "<br>")

Response.Write( FullURLFromVirt ualPath("~/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.ToS tring()
and
Request.Url.Abs oluteUri.ToStri ng()

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.itw rote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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 FullURLFromVirt ualPath(ByVal VirtualPath As String) As
String

With HttpContext.Cur rent.Request

Dim Port As String = .ServerVariable s("SERVER_PORT" )
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariable s("SERVER_PORT_ SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
.ServerVariable s("SERVER_NAME" ) & Port

Dim LocalPath As String
If VirtualPath.Tri mStart(" ").StartsWith(" ~") Then
LocalPath = (.ApplicationPa th &
VirtualPath.Sub string(1))
Else
LocalPath = VirtualPath.Rep lace("//", "/") '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.EventArg s) Handles Me.Load

Response.Write( FullURLFromVirt ualPath("//~/App_Data"))
Response.Write( "<br>")

Response.Write( FullURLFromVirt ualPath("/TechnicalPrevie w/"))
Response.Write( "<br>")

Response.Write( FullURLFromVirt ualPath("~/App_Data"))
Response.Write( "<br>")

Response.Write( FullURLFromVirt ualPath("// "))
Response.Write( "<br>")

Response.Write( FullURLFromVirt ualPath("//~"))
Response.Write( "<br>")

Response.Write( FullURLFromVirt ualPath("~/Images/Hi.jpg"))
Response.Write( "<br>")

End Sub
'Based ob Juan observation:

Function FullURLFromVirt ualPath(ByVal VirtualPath As String) As
String

With HttpContext.Cur rent.Request

Dim Parts() As String =
Request.Url.Abs oluteUri.ToStri ng.Split("/"c)
Dim Protocol As String = Parts(0)

Dim MoreParts() As String = Parts(2).Split( "/")
Dim Server As String = MoreParts(0)

Dim ProtocolAndServ er As String = Protocol & "//" & Server

VirtualPath = VirtualPath.Tri m

If VirtualPath.Con tains("~") Then
VirtualPath = VirtualPath.Rep lace("~",
..ApplicationPa th)
End If

Dim FullURL As String = ProtocolAndServ er & "/" &
VirtualPath.Tri mStart("/")

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( FullURLFromVirt ualPath("~/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 FullURLFromVirt ualPath,
to use Request.IsSecur eConnection to determine if http or https needs to be used.

That way, instead of the needlessly complex :
Dim Port As String = .ServerVariable s("SERVER_PORT" )
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariable s("SERVER_PORT_ SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If

...you'd simply use :

If Request.IsSecur eConnection 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_NAM E") and ("SERVER_POR T") ]
you might find it more economical to load the server name with Request.Url.Hos t.


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.itw rote in message
news:11******** *************@i 3g2000cwc.googl egroups.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.EventArg s) Handles Me.Load

Response.Write( Request.Url.ToS tring())
Response.Write( "<br>")

Response.Write( Request.Url.Abs oluteUri.ToStri ng)
Response.Write( "<br>")

Response.Write( FullURLFromVirt ualPath("~/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.ToS tring()
and
Request.Url.Abs oluteUri.ToStri ng()

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.itw rote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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 FullURLFromVirt ualPath(ByVal VirtualPath As String) As
String
>
With HttpContext.Cur rent.Request
>
Dim Port As String = .ServerVariable s("SERVER_PORT" )
If (Port Is Nothing OrElse Port = "80" OrElse Port = "443")
Then
Port = ""
Else
Port = ":" & Port
End If
Dim Protocol As String =
.ServerVariable s("SERVER_PORT_ SECURE")
If (Protocol Is Nothing OrElse Protocol = "0") Then
Protocol = "http://"
Else
Protocol = "https://"
End If
Dim ServerUrl As String = Protocol &
.ServerVariable s("SERVER_NAME" ) & Port
>
Dim LocalPath As String
If VirtualPath.Tri mStart(" ").StartsWith(" ~") Then
LocalPath = (.ApplicationPa th &
VirtualPath.Sub string(1))
Else
LocalPath = VirtualPath.Rep lace("//", "/") '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
25662
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 "AppDomain.CurrentDomain.BaseDirectory" but it's return only the path to my WebApplication, ex : D:\myWebSite\firstDotNetWebApp\
4
7581
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. Thanks,
14
12729
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 Web Sharing is active. For example: my application is in "C:\Inetpub\wwwroot\MyApp", the Virtual folder is in "C:\VirtualFolder", and IIS sees it as if it were in "C:\Inetpub\wwwroot\MyApp\VirtualFolder".
2
1505
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
4071
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 class library. How can I do this so I wont have to hardcode this path in my getpath() method. Here is the path below. "C:\Inetpub\wwwroot\Web_mycontrol\my_Files_dir\" -- Dotty
4
1497
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 services *. I would like to be informed that the framework provides a function to do that, but for now I am missing it.
8
2602
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 path can take the form of the following: 1. /directory/images/xyz.gif (works fine) 2. http://localhost:1234/www.mytestwebsite.com/directory/images/xyz.gif (doesn't work) 3. http:///www.someremotesite.com/directory/images/abc.gif (doesn't work)
4
8734
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 \\MyServer\Shared\SomeDirectory\SomeFile.txt. I need to convert this path to a virtual path with the virtual directory name. The result I'm after would be: MyVirtualDirectory\SomeDirectory\SomeFile.txt
4
3341
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 creating a new domain). I tried to create a Virtual directory inside my stf site so that I would access it like: www.stf.com/stage/. I run as www.stfstage.com fine and have for a long time.
0
9462
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10287
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9922
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8951
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6721
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5367
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3621
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2859
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.