hi! I haven't been here in a while but I like the changes! I've asked this question @ asp.net forums, but they were completely unhelpful. Maybe it's just me but they could not give me a straight answer. So maybe I'll have better luck here; I find the people in this community very friendly and I don't know why I didn't come here in the first place! here goes:

why does visual studio .net insist on creating new asp.net web applications in a subdirectory of the root? this poses a huge problem because I want to be able to have links and images relative to the site root but the site root is located at c:\inetpub\wwwroot. makes the root of my asp.net web site c:\inetpub\wwwroot\Project1\ which is fine except if I make a link something like this

<a href="/subdirectory/index.aspx">link</a>

it changes my link to http://localhost/subdirectory/index.aspx instead of http://localhost/project1/subdirectory/index.aspx.

so here is my simple question, how do I get around this straight and narrow path to insanity? Thank you!

-SelArom

Dani AI

Generated

Short version: Visual Studio deliberately creates a separate virtual directory (a project folder under the web-server root) by default so each web project is isolated. That is why an anchor starting with a leading slash — e.g. /subdirectory/... — ends up at the site (server) root rather than the application root. That behavior is what 's suggestion (placing the project at the server root) changes, but it isn't the only or best fix.

Better approach: use application‑relative paths (the "~" token) so links and image paths always resolve to the current app's root regardless of whether the app lives at /, /Project1, or a subfolder. Examples for ASP.NET Web Forms:

<asp:HyperLink runat="server" NavigateUrl="~/subdirectory/index.aspx" Text="link" />

<a href="<%= ResolveUrl("~/subdirectory/index.aspx") %>">link</a>

<img src="<%= ResolveUrl("~/images/logo.png") %>" alt="logo" />

Notes and alternatives: the ~ token is resolved only when the page is processed by ASP.NET (server controls, ResolveUrl, VirtualPathUtility.ToAbsolute, etc.). For purely static HTML pages (no server processing) use document‑relative links (no leading slash) or set a <base> tag in the head — but the <base> tag can have wide side effects on CSS/JS and must be used carefully. Hardcoding /MyApp/... works (as mentioned) but is less portable.

Troubleshooting checklist: confirm the IIS virtual directory actually points to the project folder; verify pages are processed by ASP.NET if using ~; and prefer application‑relative URLs for portability so links behave the same in development and after deployment.

Recommended Answers

All 3 Replies

Hi SelArom,

An ASP.NET application can be created in the root.

1) In the New Project Window, when you select ASP.NET Application you are prompted for a location. VS.NET may suggest a location like http://localhost/WebApplication1. Just delete the /WebApplication1. VS.NET will create the project in the root with a default name like localhost.

2) Right click the project in the Solution Explorer and rename it to whatever you want.

Hope this works for you.

Cheers,
- Steve

why does visual studio .net insist on creating new asp.net web applications in a subdirectory of the root?

-SelArom

HAH!! you know, I've been struggling with this for almost a YEAR. I couldn't figure out how your were supposed to do ANYTHING if your site is not in the root! I asked at asp.net and they gave me a bunch of crap about editing the HOSTS file and using if then statements to create dynamic links and NOBODY told me I could do this!

And before you say "duh it was obvious" I had actually tried to erase the project name and leave it at "http://localhost/" but when I erased the project name the "OK" button disabled and I couldn't continue. It turns out I also have to erase that last "/" for it to work!

Steve, you are my hero!

-SelArom

Hi SelArom,

Glad I could be of service. :D

Personally, I rarely develop a web app at the root level. I always create it in a subdirectory such as "MyApp". To reference images, etc. I use a root based reference like /MyApp/images/clear.gif. When I deploy to a server I will usually deploy it as the folder MyApp under the root and all the references still work.

Sometimes, you want to deploy to the root, however, and in that case, it would be most convenient if you were also developing your app at the root level.

Cheers,
- Steve

I couldn't figure out how your were supposed to do ANYTHING if your site is not in the root!
-SelArom

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.