Remember that a web "site" is still an application, it's just the application at the root of the site.
When you develop in Visual Studio, as you have found, you usually work in a virtual directory. There is absolutely nothing inherently wrong with this method. I develop my web site in a virtual directory but it's hosted at the root of a web URL (www.geekdork.com
). The key is what you use for your URLs.
In .net you can build a URL in a server control like this: "~/myresource" When .net constructs the control, it replaces the ~ with the full path of the virtual directory (web application). Therefore, if you are developing here:
you can make a link like this:
and when the page is rendered, the actual link in the href will be this:
This applies to anything that uses a URL:
<asp:hyperlink runat="server" href="~/mypage.aspx" />
User controls registration directives:
<%@ Register tagprefix="tagprefix" Tagname="tagname" Src="~/myusercontrol.ascx" %>
So on and so forth. This will allow you to use root relative references without the need to write in the determination of whether you are running in a virtual directory or not.
If you want to use this functionality programmatically, there is a method on the page class for just this purpose: Page.ResolveUrl(). Just pass it the root relative URL with the ~ in it and it will give you back the complete URL with the applicable application path.
Work smarter, not harder