How do I get the virtual root in a C# asp.net application

R

Ryan Taylor

Hello.

I am writing an ASP.NET C# application. I have made a user control so that I
can reuse some code. However, I need to be able to determine what the
virtual root of the website is, so that I can dynamically generate the
proper links to other pages on the site. Basically, I have the following
site layout. I am developing the site in a virual directory on localhost
before it is deployed to an actual site, so I need the solution to work in
both with localhost and with an actual website.

IE: http://www.mydomain.com/otherpages/otherpage1.aspx should return
http://www.mydomain.com
http://localhost/mydomain/otherpages/otherpage1.aspx should return
http://localhost/mydomain

http://www.mydomain.com or http://localhost/mydomain
- login.aspx
- /scripts/
- jscript1.js
- jscript2.js
- /otherpages/
- otherpage1.aspx
- otherpage2.aspx
- /usercontrols/
- usercontrol1.ascx
- usercontrol2.ascx

How can I get the virtual root from within C#.

-Ryan
 
M

Mark Rae

However, I need to be able to determine what the virtual root of the
website is, so that I can dynamically generate the
proper links to other pages on the site.

Can't you just use relative addressing? E.g.

<root>
\controls
control.ascx
\images
image.jpg

In order for control.ascx to "see" image.jpg, it doesn't need to know it's
*actual* fully-qualified filespec within \Inetpub\wwwroot. All it has to do
is refer to it relative to its own folder i.e. ../images/image.jpg
 
S

Sergey

on aspx page you simply can use:

someproperty="<%= Request.ApplicationPath.TrimEnd('/') %>/somefolder/somefile"
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top