Determining the "root" folder of the web application

B

Boban Dragojlovic

ASP.NET creates every website as a new "project" off the default (localhost)
web site.

So, my project "Accounting" is accessed via localhost/accounting, for
example


When I deploy this to production, the application will be in its own root,
so it might be accessed via
http://www.accounting.com (or something like that).

Within my application, I at times need URLs to web forms that I know to be
in certain subfolders off the root folder.
In other words, I might need:
href="/admin/special.aspx"

Though this works in production, it doesn't work in development, since I'm
not at the "root" of the website. In development, I would need
href="/accounting/admin/special.aspx"

Sometimes I'm making this reference from another webform which is itself
nested one or two levels within the application, so I don't want to create
gnarly parent paths like
href="./../../admin/special.aspx"
since that will cause me problems when I move the current document around.
I'd rather just provide paths off the root.

Is there a symbol or something that signifies the "root" of the current
application, as opposed to the root of the website.

How do people traditionally deal with this problem?
 
J

James Williams

Put a tilde in front of the path.

If the path is "admin/special.aspx", then make it "~/admin/special.aspx".

This works for production and development. When it runs on production, it
will translate to "/admin/special.aspx" and when it runs on development, it
will become "/accounting/admin/special.aspx". This only works with ASP.NET
controls, not with standard HTML controls.

The tilde basically tells the ASP process to fetch the root application path
for you.

-James
 
B

Boban Dragojlovic

Thanks.

Yes, that does work for ASP controls, but not for basic HTML, and I
frequently have hyperlinks from one page to another, and I'd rather not have
to make them server controls
- because of the extra (unnecessary overhead)
- because I often build a set of URLs dynamically, and it would be a
REAL pain to create them as ASP controls
 
J

James Williams

Well, if you are using code behind and are using an inherited base page for
all existing pages, you could add a public property to the base page as
such:

public string AppPath
{
get
{
#if DEBUG
return "/accounting/";
#else
return "/";
#endif
}
}

Then, on the HTML page, instead of having a link like this for development:
<A href="/accounting/admin/special.aspx"></A>
....and a link like this for production:
<A href="/admin/special.aspx"></A>
...you could have one link to rule them all:
<A href="<%=AppPath%>admin/special.aspx"></A>
 
M

M. Shawn Dillon

You can also programmatically retrieve the current application root to
prepend to the path via a call to Request.ApplicationPath. This returns the
virtual root of the application, rather than the filesystem path (which is
available as Request.PhysicalApplicationPath).

HTH,
M. Shawn Dillon
Senior Application Developer
MaximumASP, LLC
 
M

Michael Stuart

The property sounds like a good idea! I would probably just store the
application's root path in the web.config file instead of hard-coding it
though...then just have different path values dev and production.

The following is not as good, but thought I'd post it anyway just as
another option:

If you have windows server, I think you can create new webs in IIS and
assign different port numbers to them. So you could then access your
development web like "http://localhost:81/Default.aspx"(you can then
start all your links with the root "/" character), instead of having to
create a web app under your root, where it would be
"http://localhost/accounting/Default.aspx"(all your links would start
with "/application/" on development). I don't know how to create new
webs with port numbers like this is IIS without windows server though.

If you don't have server, a hack to simulate this would be to just
manually change your root website's home directory path to point to the
folder that your application is located in. The drawback is that if you
have other applications you work with, you'd have to change the home
path before working with each one(it would be annoying).

The drawback to this whole option is that you cannot use absolute links
in your application...they would all have to start with "/"(or at least
with the html controls). Using ASP.net and page inheritance, the
previous post by James sounds like the way to go.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top