Getting the application path

W

Water Cooler v2

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.
 
M

Mark Rae

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();
 
J

Juan T. Llibre

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.
 
B

Ben Amada

Juan said:
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
 
J

Juan T. Llibre

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.
 
B

Ben Amada

Juan said:
re:

They're not.

re:

/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
 
J

Juan T. Llibre

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top