473,387 Members | 1,834 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,387 software developers and data experts.

Getting the application path

How does one get the root of the ASP.NET application. For instance, my
application is:

http://localhost/MyApplication/login.aspx

Today, it is on a test server, but when I move it to the production
server, it would become something like:

http://www.foobar.com/MyApplication/login.aspx

From within one of the pages, how do I get the correct path of the

application so that I don't have to change hard-coded values everytime.
I need the entire path and not the relative to the page from which I
query the path.

Nov 19 '05 #1
6 1767
"Water Cooler v2" <wt*****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
How does one get the root of the ASP.NET application.


Have a look at the ServerVariables collection, specifically:

HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString();
HttpContext.Current.Request.ServerVariables["PATH_INFO"].ToString();
HttpContext.Current.Request.ServerVariables["PATH_TRANSLATED"].ToString();
HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString();
HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString();
Nov 19 '05 #2

Use a relative path beginning with a tilde and a slash :

The tilde ( ~ ) stands for the application's root directory.

"~/login.aspx" will always refer to a login.aspx located in
the application's root, no matter what the domain name is.

You can use the same reference for subdirectories :

"~/subdir/newpage.aspx" refers to a page named "newpage.aspx"
which is located in the "/subdir" subdirectory of your App.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Water Cooler v2" <wt*****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
How does one get the root of the ASP.NET application. For instance, my
application is:

http://localhost/MyApplication/login.aspx

Today, it is on a test server, but when I move it to the production
server, it would become something like:

http://www.foobar.com/MyApplication/login.aspx


From within one of the pages, how do I get the correct path of the

application so that I don't have to change hard-coded values everytime.
I need the entire path and not the relative to the page from which I
query the path.

Nov 19 '05 #3
Juan T. Llibre wrote:
Use a relative path beginning with a tilde and a slash :

The tilde ( ~ ) stands for the application's root directory.

"~/login.aspx" will always refer to a login.aspx located in
the application's root, no matter what the domain name is.

You can use the same reference for subdirectories :

"~/subdir/newpage.aspx" refers to a page named "newpage.aspx"
which is located in the "/subdir" subdirectory of your App.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/


Just curious, but I thought ~/login.aspx and /login.aspx were the same ...
no?

What is the difference between the two? Are there any disadvantages to
using /login.aspx? (without the tilde)

Thx,
Ben
Nov 19 '05 #4
re:
Just curious, but I thought ~/login.aspx and /login.aspx were the same ... no?
They're not.

re: What is the difference between the two?
/some.aspx is relative to the current directory.
~/some.aspx is relative to the application root directory.

This becomes particularly important when the links are
written into a user control, because the link's reference
will be relative to the directory the user controls are in,
and not to the directory the aspx file is located in.


Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Ben Amada" <be*@REpoMOweVErpick.com> wrote in message
news:uq**************@tk2msftngp13.phx.gbl... Juan T. Llibre wrote:
Use a relative path beginning with a tilde and a slash :

The tilde ( ~ ) stands for the application's root directory.

"~/login.aspx" will always refer to a login.aspx located in
the application's root, no matter what the domain name is.

You can use the same reference for subdirectories :

"~/subdir/newpage.aspx" refers to a page named "newpage.aspx"
which is located in the "/subdir" subdirectory of your App.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/


Just curious, but I thought ~/login.aspx and /login.aspx were the same ... no?

What is the difference between the two? Are there any disadvantages to using
/login.aspx? (without the tilde)

Thx,
Ben

Nov 19 '05 #5
Juan T. Llibre wrote:
re:
Just curious, but I thought ~/login.aspx and /login.aspx were the same
... no?


They're not.

re:
What is the difference between the two?


/some.aspx is relative to the current directory.
~/some.aspx is relative to the application root directory.

This becomes particularly important when the links are
written into a user control, because the link's reference
will be relative to the directory the user controls are in,
and not to the directory the aspx file is located in.


I've never used the tilde and never had problems not using it -- probably
because I've never used a usercontrol. At any rate, thanks again for making
me aware of the difference :)

Ben
Nov 19 '05 #6
Hi, Ben.

re:
I've never used a user control.
You *should* get into user controls.

They are quite useful and can save you a lot of coding time,
especially when it's time to reuse code/functionality.

Also, they serve as substitutes for the good 'ol include files.

There's an additional place where a URL starting with a tilde is very useful.

If you use error trapping in web.config, the error page /YourErrorPage.aspx,
is relative to the Web.config file that specified the URL for this attribute,
not to the Web page in which the error occurred.

If you have multiple web.config files, which might be nested deep
in a subdirectory tree, you're going to get into trouble if you
don't use an URL prefaced with a tilde.

Again, a URL starting with a tilde (~), such as ~/YourErrorPage.aspx,
indicates that the specified URL is relative to the root path of the application.

That is quite handy.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Ben Amada" <be*@REpoMOweVErpick.com> wrote in message
news:uO*************@tk2msftngp13.phx.gbl... Juan T. Llibre wrote:
re:
Just curious, but I thought ~/login.aspx and /login.aspx were the same
... no?


They're not.

re:
What is the difference between the two?


/some.aspx is relative to the current directory.
~/some.aspx is relative to the application root directory.

This becomes particularly important when the links are
written into a user control, because the link's reference
will be relative to the directory the user controls are in,
and not to the directory the aspx file is located in.


I've never used the tilde and never had problems not using it -- probably because I've
never used a usercontrol. At any rate, thanks again for making me aware of the
difference :)

Ben

Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohan | last post by:
Hi All, I am using web browser control in a VB 6.0 client server application to print reports. I am writing the report into a HTML file and displays the report to the user using the code,...
11
by: KarimL | last post by:
Thanks for your advices... but i need to get the Image height because i dynamically resize the height of my webcontrol based on the image height. More i just have the url (relative parth) to the...
1
by: Hitesh Panchal | last post by:
Hi, I have a ASP.NET application. IN which i have included one xml file. Now to getting values from that XML file if I am going through Load Method of XMLDOC it is fetching file from...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
1
by: Raed Sawalha | last post by:
I have a problem in getting asp.net application physical path in global.ascx static function in my global.ascx i have a static function : first i tried to get the physical path by refelection...
8
by: bryan | last post by:
Is there any way I can get the application path (the one returned by Request.ApplicationPath) in the Application_Start method in Global.asax? Request is not valid there. On a related note, is there...
11
by: Geoff Jones | last post by:
Hiya I have written a small application which I've then generated a setup project to install the application on other computers. How can I determine, from the application, where the program...
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...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.