Tip/Trick: How to Run a Root “/” Site with the Local Web Server using VS 2005 SP1

One of the questions I'm often asked is whether it is possible to run an ASP.NET web-site project as a top-level root "/" site using the built-in VS web-server and the VS 2005 Web Site Project model.

By default, when you open a web-site as a file-system based web site project and run it, VS will launch and run the built-in web-server using a virtual app path that equals the project's root directory name.  For example: if you have a project named "Foo", it will launch and run in the built-in web-server as http://localhost:1234/Foo/  What a lot of people want to-do instead is to just run the web-site as http://localhost:1234/ or (if port 80 isn't already in use): http://localhost/  Doing this can make site navigation and url handling logic much simpler in your code.

Prior to VS 2005 SP1 being released, I would recommend either running the project using IIS instead (here is a past post of mine on using the web-site project model with IIS), or to use a blog of mine from a year ago that discusses how to use the "external tools" feature in VS to enable root web-sites to accomplish this.  The good news is that VS 2005 SP1 makes this even easier with the built-in VS web-server.

Step by Step Instructions on Configuring a VS 2005 Web Site Project to Run as a Root "/" Site

The below steps walkthrough how to configure a VS 2005 Web-Site Project to run as a root "/" web-site:

1) Open up an existing web-site project or create a new one by selecting the File->New Website menu item.

2) Using the solution explorer within Visual Studio, select the web-site project node:

3) Go to the property-grid within the IDE, which (if the project root node is selected) will now display the project properties for the web-site.  There are three relevant properties that we care about for the purposes of this tutorial: "Virtual path", "User dynamic port", and "Port Number".  Change the "virtual path" setting to / to run as a root web-site.  You can also then set the dynamic port setting to "false" and configure a specific port to use (for example: port 8081 or port 80 if it isn't already in use):

4) Now click on a page within the project and run it.  You will see that the web-server was launched as a root "/" site:

Note that the :8081 was added at the end of http://localhost because I already have IIS7 running on my Vista machine and it has a site using port 80.  If I disabled IIS I could have configured the web-site project to use port 80, in which case the web-browser would just have http://localhost/ in the address bar. 

I can now perform root-relative navigation within my sitemap (example: /products, /help, etc), as well as in my redirect logic and with standard HTML elements (example: <a href="/path">).  I can also now reference javascript files relative from the root, for example: <script src="/js/library1.js"></script>.

Tips/Tricks for Handling CSS StyleSheets with VS 2005 and ASP.NET 2.0

One of the techniques I recommend taking advantage of with ASP.NET 2.0 when using CSS is to use the Master Page feature to provide a consistent UI across your site, and to reference all stylesheets in one place using a master page (all pages based on the master will then automatically pick them up). 

One tip to take advantage of is the relative path fix-up support provided by the <head runat="server"> control.  You can use this within Master Pages to easily reference a .CSS stylesheet that is re-used across the entire project (regardless of whether the project is root referenced or a sub-application):

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>

<html>
<head runat="server">
    
<title>Master Page</title>
    
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</
head>
<body>
    
<form id="form1" runat="server">
    
        
<asp:contentplaceholder id="MainContent" runat="server">
        
</asp:contentplaceholder>
    
    
</form>
</body>
</html>

The path fix-up feature of the <head> control will then take the relative .CSS stylesheet path and correctly output the absolute path to the stylesheet at runtime regardless of whether it is a root referenced web-site or part of a sub-application. 

The pages within your web-site can then look like the content below and automatically pick up the stylesheet settings (both at runtime at a design-time within the VS HTML WYSIWYG Designer):

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Sample Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">

    
<h1>Root Web Site Sample</h1>
    
    
<a href="/Products">Click here to go to the Products section (note the absolute path) </a>

</asp:Content>

Because image references contained within a .CSS stylesheet are referenced by a browser relative to the path of the .CSS file (and not actually the path of the page the stylesheet is being used on), you can combine this behavior with the <head runat="server"> logic above to have your images automatically work both with root web-sites and sub-applications (and not break if you change paths later). 

Images referenced this way will also show up fine within the VS 2005 HTML WYSIWYG designer (which can otherwise sometimes have trouble determining the root "/" path to pick up image references).

Hope this helps,

Scott

P.S. The Web Application Project Model within VS 2005 also has full support for root "/" web projects and the VS web-server (WAP projects by default are root relative).  To configure the path settings using a web application project, right-click on the web project node in the solution explorer, select "properties", and then click the "web" tab to configure them.

P.P.S. Click here to read more of my ASP.NET 2.0 Tips, Tricks and Gotcha posts.

57 Comments

  • This is awesome. Setting it up in IIS was a royal pain - this makes life a lot simpler.

  • Very valuable information. Thanks much!

  • That's a great feature to have! Is there a document anywhere that lists all of the new features in SP1?

  • Have there been any enhancements to the "Copy Web Site" tool? I've never seen any way to configure this for the type of comparison I want or any way to exclude certain files (Like do not publish my dev web.config to my staginging webserver).

  • Hi Tom,

    You can definitely use the ResolveClientUrl method like that in both .aspx/.ascx markup as well as in your own control logic (the method is built-in to every control). It provides a really useful way to come up with client references to urls.

    Thanks,

    Scott

  • Hi Dan,

    We are putting together a comprehensive list of all SP1 changes now, and will probably have one posted in early Jan.

    For web scenarios, Omar from the web tools team is going to try and blog the list of web tool specific changes today or tomorrow on his blog here: http://blogs.msdn.com/webdevtools/

    Hope this helps,

    Scott

  • THANKS!!! I was using the previous method you had talked about and after installing SP1 I no longer could use the root directory trick. &nbsp;I kept getting a directory not found error. &nbsp;Now everything is back as it should be and it&#39;s easier to implement which is awesome!

  • Hi Dave,

    Unfortunately there haven't been any major enhancements to the "Copy Web Site" tool with SP1 (other than a few bug fixes). It doesn't provide more advanced filtering of certain files yet.

    One thing you might want to look at is using either the web application project or web deployment project for this. You can mark files in your project as non-content files - in which case they won't be deployed when you do a publish.

    Hope this helps,

    Scott

  • That&#39;s it. &nbsp;I think Scott has like 12 interns working for him writing blogs all the time. &nbsp;No way does he have the time to get all this stuff done.
    Just like dwahlin, I would also like a list of all the new features in sp1, especially the ones that pertain to asp.net

  • Hi npearson99,

    No interns helping me blog I'm afraid - I actually write and come up with every post. :-)

    I'm officially on vacation this week, which is why I've had to blog more than usual this week.

    Thanks,

    Scott

  • I'm not sure if this is off topic but... When using a WAP setup with sub projects (with IIS(that's my tie in to the thread ;) )) Can you put a web site project (file based) in as a sub project of a WAP set up?

    Also, can you have subproject level web.config's (for use in application or sub project level authentication), regardless of the site architecture method? The threads all seem to point to having one root level web.config (or should you just have one crazy big root config?)

    Thanks,

    Eric

  • This is exactly the solution I was looking for -- but something is preventing me from trying it. My Visual Studio 2005 Pro does not have a "Virtual path" property. I checked another computer running VS2005 Team System and it was also missing the property. Any info on this would be great.

  • I installed SP1 and when I viewed the Property Grid for my project, the new options did not appear. I could not figure it out. I opened one of my projects which has 3 separate web applications, and tried each one - then I saw that it worked for two of the projects, but it would not show the Port Number/ virtual path options for one of the projects.

    Then I realized that for that one project I had the project "Property Pages" - Start Options set to "Use custom server" so I could use the 8080 external tools trick.

    I set that back to use default Web server and Voila - the new properties appeared in the grid.

    I just thought I would share this with the rest of you to save some time and hassle.

    Kevin

  • Scott, is there any way to have the built-in web server ignore specific file types from authentication? I have a web site with a login screen, but the CSS file is never delivered because the web server protects the file due to my security rule in web.config.

  • Hi Shane,

    Can you double check that you have SP1 installed and are using a VS 2005 Web Site Project? Also - can you check to see whether you might be running into the issue Kevin described above (where the custom server option is checked - in which case those properties aren't present)?

    Thanks,

    Scott

  • I've successfully installed SP1 and the Virtual Path property is indeed there. I changed it to "/" and my site full of absolute paths shows up correctly in a web browser via the built-in web server. However, Visual Studio's Design View still can't find any of my images or stylesheet references. I tried restarting VS to no avail. I was hoping this new Virtual Path property would fix all my broken images, and my search for a solution to this over the last few weeks has been pretty fruitless. Any ideas I could try out?

  • Scott or readers of this blog,

    I am very interested in hearing from you how to accomplish nested websites. Right now I have 1 solution with 4 websites. One is in the IIS root and 4 are in virtual folders marked application in IIS. These 4 all have a virtual folder that points to a folder called MasterPages in the IIS root.

    Using this setup I can share master pages and configure single sign-on. (Because the web.config cascades, sweet!)

    Anyway, I want to do this exact structure using the built-in web server. Is that possible?

    If not, is this scenario considered for Orcas?

    Happy holidays Scott!

  • Scott,
    So if you have multiple web apps in a solution that need to talk to one another, would IIS could cassini run them simultaneously (concurrent)? i.e. a web app project that needs to talk to a web service project in the same solution.
    What if the apps were in seperate solutions?

  • nice scott, thanks

  • Thanks for this update. This is really cool for testing the web sites

  • Hi Brad,

    If you have multiple independent web-sites in your solution, you can configure all of them to run simultaneously - in which case you can definitely have web-services talk to each other.

    Hope this helps,

    Scott

  • Hi Shane,

    When you do "/" references for images or stylesheets, VS will look at the root path of your drive for the files (which in most cases is not the behavior you want).

    If you follow my tip/trick above you can reference stylesheets relatively using a master page and ASP.NET will automatically fix up the path for you (and support showing it in design-view). Images referenced via the stylesheet will also then be fully supported in design-view as well.

    Hope this helps,

    Scott

  • Hi Foobar,

    With regard to your question about authorization rules with Cassini (the built-in webserver), I think this blog post should help you with your scenario: http://weblogs.asp.net/scottgu/archive/2006/01/31/437027.aspx

    Hope this helps,

    Scott

  • Hi Mike,

    Depending on your file-structure, you should be able to implement nested projects using the built-in webserver.

    One difference with the built-in web-server and IIS is that the built-in web-server doesn't support the concept of "virtual directories" - which means that to have a sub-url entry it needs to be a sub-directory underneath the root project. As long as your sub-projects are structured on disk like this then everything should work.

    The IIS virtual directory feature is very useful, though, for cases where you don't want the project directories to be underneath a root one. In this case you could have them anywhere on disk and just use IIS to connect them up.

    For more information on creating/using sub-projects with Visual Studio, I'd also make sure to check out this blog post: http://weblogs.asp.net/scottgu/archive/2006/08/16/Tip_2F00_Trick_3A00_-Creating-Sub_2D00_Web-Projects-using-the-VS-2005-Web-Application-Project-Option.aspx

    Hope this helps,

    Scott

  • Hi Eric,

    I just answered a similar question to your sub-project one above for Mike that you might want to check out.

    Also - have you reviewed this blog post: http://weblogs.asp.net/scottgu/archive/2006/08/16/Tip_2F00_Trick_3A00_-Creating-Sub_2D00_Web-Projects-using-the-VS-2005-Web-Application-Project-Option.aspx

    Hope this helps,

    Scott

  • Scott,

    Thanks for the reply. The problem I was having with using site root-relative resources such as "/images/image.jpg" or "/styles/style.css" (which would break in Design View) was caused by using the File System project type for my new web sites. I've switched over to HTTP and now everything behaves normally. I caution any other ASP.NET newcomers to avoid doing what I did!! It took me a long time to find this solution. Is such behavior a bug or intentional? Thanks again for all your help.

  • Scott, vacation means you DON'T work :).

    I really love the blogs though. Your my favorite asp.net blog.

  • How about .js files. I have a common javascript file that I want all pages to have acces to. I have it in my master page and it works fine for all page in the root - but any pages that are in a folder under the root do not have access to the .js file?

  • Scott:

    A follow up to Shane's question. I want to use root "/" for images and style sheets outside of a master page. When I do the VS designer doesn't like it.

    For example, inside the body I have the following:


    This fails to display in the designer, any thoughts? Is there a way to set what the designer thinks is the root? I've noticed some settings in the Solution file...

    Thanks again for this post.

    Josh.

  • Scott,

    Ah, thanks for the article. It's not a perfect solution but it'll do.

    Also, one thing I haven't seen mentioned anywhere: Intellisense now works in skin files! Woo hoo!

  • Scott:

    How do you use a root reference "/" (outside of Master Pages) with the src property of an image and still have it display in the designer? For example:


    will not display in the Visual Studio 2005 SP1 Designer. But if you change it to a relative path:


    it displays.

    You mention the virtual root for the designer is in a different location. Is it in the solution file or stored somewhere else that can be updated to make this work?

    Thanks again for the posts!

    Josh.

  • Scott. Im glad the root website capability made it into the service pack. This is a big help. Im a little bummed about absolute image references not showing up in the designer though. Your proposed workaround is ok, but its not typical to put every image reference in a stylesheet is it?

    Seems like designer support for absolute paths would be such a trivial thing to implement but I guess it must be more complicated than I can imagine since it dint make it into the service pack.

    Besides the image problem, are there any other advantages to using an IIS website over a file system website? It appears they are pretty much functionaly equivalent now.

    Thanks again.

  • Thanks Scott
    Will this work with web developer express addition.
    rgds
    Rob

  • Hi Rob,

    Yep - this will work with the Visual Web Developer Express edition as well.

    Thanks,

    Scott

  • Scott i tried to post 5 times and i never see my post, can I email my question?

    Thanx.

  • Hi Paul,

    I'm not sure why your post isn't showing up. Feel free to email me directly (contact details are under the "about" link at the top of this page).

    Thanks,

    Scott

  • Hi Brian,

    That is odd - you should definitely see this option if you have SP1 installed. Can you try selecting the Help->About menu item and scroll down in the list of installed products to see if SP1 is listed?

    Thanks,

    Scott

  • I also was getting an error after upgrading Web Developer Express to SP1 that "such and such directory does not exist". After fidgeting around with the External Tools settings, I found that if you place a final space and a slash after in the arguments, like this:
    /port:8080 /path:$(ProjectDir) /
    the problem is solved.

    Chris

  • Image references aren't showing up in the designer. Your proposed workaround is ok, but its not typical to put every image reference in a stylesheet is it?

  • This is on the spot what i was lookin for.
    Bad news (for me) is that i can't get it work.

    Upgraded to SP1 (Microsoft Visual Studio 2005
    Version 8.0.50727.42 (RTM.050727-4200)
    Microsoft Visual Studio 2005 Professional Edition - ENU Service Pack 1 (KB926601))
    But i can't se the "virtual path". If i create a http-base website the properties are totally different.

    I this only available in Vista?

    /Mats

  • Hi Mats,

    Can you use the technique in this blog post to double check that you have SP1 applied: http://weblogs.asp.net/scottgu/archive/2007/01/01/a-few-vs-2005-sp1-links-and-information-nuggets.aspx

    You should then be able to set this property within the property grid of a file-system based web project on all versions of Windows.

    Thanks,

    Scott

  • I'm creating a website that will support themes. I'm creating the first default theme.
    As far as refering to background images on CSS using url('myimage.gif') everything is fine.
    But I have some images on the front pages that are simply being linked as images, using
    They look like:


    But so, now, when someone create and uses another theme, it will not pickup the corresponding images, but it will be always pointing to the default theme.
    How do I do to get these images dinamycally pointing to the current theme directory ?

  • I don't know if it is my instalation or what, but the Publish Website option seems to don't like this configuration.
    When I press Publish I get an error that says "Build failed", but no error list is given.
    Changing the virtual path from "/" back to something like "/mysubdirectory", make it work again.
    Does anyone else is having this ?

  • Excellent. Thanks for adding the ability to configure this.

  • Hi Cesar,

    I haven't seen that problem before. Can you send me an email with the details of your configuration? I'll then have someone on the team take a look to figure out what is going on.

    Thanks,

    Scott

  • I have an issue with using a Master page from a virtual directory. The pages work fine, but Intellisense doesn't work on any of the aspx's that reference the Master from the virtual directory. I get "the '~/SharedControls/Site.Master' File Does Not Exist" error on the page declaration. Is there any way to make this work.


    We have several applications using a common website (control library) set up as a virtual directory under each application. This common site holds all of the master, images, styles etc. This strategy seems to be working well, but the lack of IntelliSense is killing me.

    Any thoughts? Thanks,
    Jay


  • Like Mats - the property pages are not showing - the install went smoothly and appears in help->about.

    With either existing web projects or new ones the property list has just :
    MISC
    - full path
    - Opened URL
    POLICY
    - Policy File

    Thankfully the trick mentioned above of adding a space and a trailing / to the old method got it to work for me - phew, i dont have time to re-install atm :) :)

    Cheers
    Shane

  • Is there any way to programatically stop (from the command line) the WebDev.WebServer.exe process?

    Cheers,
    Simondo.

  • Hi Simondo,

    The only way I can think of to stop it would be to do a kill process on the process ID of webserver.

    This tip/trick of mine should help with this: http://weblogs.asp.net/scottgu/archive/2006/07/01/Tip_2F00_Trick_3A00_-Command_2D00_line-Tasklist_2F00_Taskkill-Utilities-.aspx

    Thanks,

    Scott

  • Thanks Scott
    My command prompt didn't recognize 'taskkill.exe', but 'tskill.exe' did the trick.
    Thanks again,
    Simondo.

  • Scott, I saw you mention that SP1 now supports intellisense in skin files. Is that something that you have to turn on and if so how? I've installed SP1 but not getting intellisense in the skin files.

    Thanks
    J

  • Hi Jerome,

    You shouldn't need to configure anything to enable this. Are you not seeing it show up?

    If so, send me email and I can try and have someone take a look at it for you.

    Thanks,

    Scott

  • Scott,
    I tried to find an answer for thisquestion for some time with no luck. What is "override application root url" and how exactly does it work?

  • Hi Alex,

    By default the built-in web-server uses a virtual root path when running an app. For example:

    http://localhost/apppath1/

    You can use the above technique to instead have it run out of the root. For example:

    http://localhost/

    Hope this helps,

    Scott

  • Just installed SP1 and the virtual path property did show. However, the entire Properties window is blank when I'm in Source view, but works properly when I'm in Design view. Anyone have this strange behavior after the SP1 installation?

  • Never mind. I found the solution by googling. SP1 installation by default turns off the Properties page because many find it annoyance rather than helpful. Had to turn it back on from Tools | Options. Why change the settings that are in place?

  • Hi Scott

    Thanks again for updating this post. I am glad to see you are running vista, like me so early since its release. Please continue to blog, your information is useful.

Comments have been disabled for this content.