How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Update: This blog post is now old and out of date.  Please read this newer post of mine instead. 

 

 

One of the questions I’ve been asked a few times is whether it is possible to build and run an ASP.NET web-app as a top-level “root” site using the VS 2005 local, built-in, web-server (which is the default web-server used when you open a web-site as a file-system based web).

By default, when you open a web-site as a file-system based web and run the project, 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/Default.aspx.

One downside to this is that it makes it hard to fully qualify things like static CSS and image files within your site (for example: using root qualified paths like /images/myphoto.jpg or /css/mycss.css).  Note that you can use the “~” app path trick for dynamic resources – but static resources still end up being a pain.

One way to solve this is to configure VS 2005 to run your site using IIS instead (and configure the app to be the root site on your IIS web-server).  You can then open and edit it just fine using VS 2005 (for details on this read: http://weblogs.asp.net/scottgu/archive/2005/08/23/423409.aspx).

Alternatively, you can use the tip/trick below to accomplish the same task with the built-in file-based web-server:

Step 1: Select the “Tools->External Tools” menu option in VS or Visual Web Developer.  This will allow you to configure and add new menu items to your Tools menu.

Step 2: Click the “Add” button to add a new external tool menu item.  Name it “WebServer on Port 8080” (or anything else you want).

Step 3: For the “Command” textbox setting enter this value: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.EXE (note: this points to the web-server that VS usually automatically runs).

Step 4: For the “Arguments” textbox setting enter this value: /port:8080 /path:$(ProjectDir)

Step 5: Select the “Use Output Window” checkbox (this will prevent the command-shell window from popping up.

Once you hit apply and ok you will now have a new menu item in your “Tools” menu called “WebServer on Port 8080”.  You can now select any web project in your solution and then choose this menu option to launch a web-server that has a root site on port 8080 (or whatever other port you want) for the project.

You can then connect to this site in a browser by simply saying http://localhost:8080/.  All root based references will work fine.

Step 6: The last step is to configure your web project to automatically reference this web-server when you run or debug a site instead of launching the built-in web-server itself.  To-do this, select your web-project in the solution explorer, right click and select “property pages”.  Select the “start options” setting on the left, and under server change the radio button value from the default (which is use built-in webserver) to instead be “Use custom server”.  Then set the Base URL value to be: http://localhost:8080/

Now, when you click on any page in the app and then hit F5 or Ctrl-F5, the IDE will use the custom web-server you launched to connect instead of popping up its own.   It will resolve all paths correctly (for example: if you select a page "Foo.aspx" under a directory "Bar" -- it will launch the browser at http://localhost:8080/Bar/Foo.aspx when you hit F5).

The good news is that you only need to follow the above steps once.  If you restart the IDE, just select your web-project and choose your web-server option from external tools to launch the web-server instance, and then hit ctrl-f5 or f5 to launch your app and you’ll be good to go.

Hope this is a helpful tip/trick,

Scott

P.S. Yes – we should have made the root web-site the default (but hopefully this tip will tide you over until we can change that)…

P.P.S. Robert also posted a cool related tip to launching the web-server from a file-system folder here: http://weblogs.asp.net/rmclaws/archive/2005/10/26/428600.aspx

Published Monday, November 21, 2005 8:44 PM by ScottGu

Comments

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 22, 2005 4:12 AM by David Taylor
>P.S. Yes – we should have made the root web-site the default

Maybe...but the benefit of what you shipped is it forced us to build apps that can handle the virtual directory being changed (or even just run from the root).

I cannot say how many times with V1.x we built sites using the default of a virtual directory, and then had a problem if the site was deployed directly to a root (or visa-versa).

So while the current config is a pain....it forces you to handle it, even if you need to do some extra work.

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 22, 2005 11:02 AM by Walter Lounsbery
Scott,

I can't thank you enough for this material! Your post is very timely as Juan T. Llibre, MVP, and I have been going back and forth on this situation on the ASP.NET forum. Juan has been very patient, but maybe didn't have access to all of this material.

Setting the root URL for VS 2005 is a major, major impact on developing websites.

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 22, 2005 11:54 AM by ML
This is awesome! Thanks.

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 22, 2005 11:55 AM by Eber Irigoyen
>>Yes – we should have made the root web-site the default (but hopefully this tip will tide you over until we can change that)…

no, I don't think you should've made it that way by default, it should be secure by default, the deploy option is excellent

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 22, 2005 12:25 PM by scottgu
Hi Eber,

Running the website as a root web-site actually doesn't change any of the security lockdown settings -- the web-server still only accepts local client requests (you can't access it remotely).

But I'm glad you like the deployment options nonetheless. :-)

Thanks,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 22, 2005 1:31 PM by Juan T. Llibre
RE : "maybe didn't have access to all of this material" : Scott only published it yesterday! :-)

This is a neat trick, indeed !


Thanks, Scott!

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 22, 2005 2:17 PM by SkidMarks
Hi Scott,

Just curious. I know that having the filesystem-based web server respond to only local requests was a security move, but I work for a corporate IT department where we have a cookie-based security system.

And, in order to test security, e need to access our sites with a "*.companyname.com" URL in order for the app to be able to read our security cookie. Is there any command line options that will enable us to start up the filesystem-based webserver so that it will at least allow it to serve to "machinename.companyname.com" style URL's...? I didn't see anything documented, but thought I'd ask...

Thanks!

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 22, 2005 2:29 PM by scottgu
Hi Skid,

I think you should be able to update your hosts file to map a server with this domain to 127.0.0.1 (which is your localhost IP address).

I think if you do this, you will then be able to access the site locally using the hostname. As long as it comes in on 127.0.0.1 you should be good to go.

Hope this helps,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 22, 2005 4:26 PM by Todd M. Taylor
Oh man, this is very cool! Especially when combined with Robert Mclaw's tool.

Note to self: Add Scott Guthrie's blog to the top of my blog-roll ;)

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Monday, November 28, 2005 11:22 AM by eTm
Please ignore my last post... I had a javascript error... sorry to bother you!

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Wednesday, November 30, 2005 8:13 PM by Stephen
Is there a way to make the "Web Server 8080 External Tool" fire up automatically upon starting Studio?

I keep forgetting to manually start it :-(

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Thursday, December 1, 2005 12:45 PM by scottgu
Hi Stephen

Unfortunately I don't know of a super easy way to auto-launch it -- although I bet there is someway (maybe a macro?).

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Friday, December 9, 2005 2:54 AM by Trevor
For some reason the 'Use custom server' option is disabled for me. Any idea what might be going on there?

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Saturday, December 17, 2005 5:42 PM by Reanuris
It works fine in a real PC but it fails in my VPC. I always receive the same error message :
"ASP.NET Development Server failed to start listening on port 80.
Error message:
An attempt was made to access a socket in a way forbidden by its access permissions"
Of course I'm not using port 80 but a free one. My VPC run Windows Server 2003 SP1. As I can't fix the problem, do you have any idea?
Thanks

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, December 20, 2005 2:29 AM by scottgu
Hi Reanuris,

Are you running IIS on that box as well? If so, then you probably already have a web-server listening on port 80. Have you tried running it on a different port instead?

Hope this helps,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, May 30, 2006 9:29 PM by Jesse
Hi Scott.  I was wondering if this technique is supposed to help with absolute image urls in the designer too.

I have succesfully configured the site to run with the built-in web server as you have described.  Resources are mapped correctly when viewing pages through the browser, but not in the designer.

Thanks for any info.

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Wednesday, May 31, 2006 2:37 AM by ScottGu
Hi Jesse,

Unfortunately the designer doesn't pick up the images correctly when using this option.  Instead, if you want to use a root url you'd need to either:

1) Use IIS with a web-site project

2) Use the VS 2005 Web Application Project option -- which does allow root urls in the project.

Hope this helps,

Scott

P.S. We are looking to make a root url configurable to support with VS 2005 SP1.

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Thursday, June 8, 2006 8:25 AM by Anja
Vielen Dank für diesen interessanten Artikel!!
Many Thanks for you - for this articel!!

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Friday, June 9, 2006 12:39 PM by Cal
Hey Scott ... first thanks for your always informative and helpful blog. I use it as a reference continually.

After following the instructions on this particular one, I was able to get my site running with the root "/" using the ASP.NET Development Server.  But, now when I try to open the Website -> ASP.NET Configuration menu item I get an error window titled "Microsoft Visual Studio", and it says "Unable to connect to Visual Studio's Localhost Web Server".  I don't I had ever tried to run this option before I made the changes you suggested in this blog, because I am used to coding the web.config file by hand like I do for ASP.NET 1.1 apps.

But, I bet this error is related to the changes that I made here.  I can't seem to find any references on the internet that seem to help ... so I was going to see what you thought.  Thanks for your time.

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Sunday, June 11, 2006 12:48 PM by ScottGu
Hi Cal,

Interesting -- I haven't heard of that error before.  I'm wondering if is related to setting the "Base URL" property on your project property pages.

Can you try unsetting this value and see if it works now?

Thanks,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Friday, July 28, 2006 5:02 PM by Mike Murkovic
I LOVE you. I had been stuck with this problem for so long. It's a real annoying problem that I found hard to phrase as a question. Then I figured it was something wonky with the ASP.NET Development Server and AHA! Here comes your article. It worked like a charm and I love you very much :)

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Thursday, August 3, 2006 3:49 AM by Ryan Haney
Speaking of which...when using the solution from my previous request while trying to obtain the host name via the request object, it always returns localhost, but if I use ServerVariables["HTTP_HOST"], it returns the domain passed by the browser. Any reason for this? Thanks!

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Friday, August 11, 2006 3:18 PM by Adam
Great post! Thought I'd offer our environment for discussion: We're upgrading from 1.1 while developing on Windows XP and deploying to Windows Server 2003. We currently use a command line batch file and WSH .vbs script to switch IIS to point at different "virtual" directories when we need to develop a different site (limitation of IIS in XP is only one website at a time). After reading more, I still think we'll be happier using IIS instead of the new ASP.Net 2.0 built-in local web server, and the same batch file .vbs script to switch between our different web site environments, even in Visual Studio 2005. That is until one of two events: 1) we start developing on Windows 2003 Server instead of XP (and thereby do away with the one website limitation and the need to switch between them) or 2) VS.Net Service Pack 1 includes designer support for images using built-in development server, in which case we can use the built-in development server. My reasoning for sticking with IIS is that the custom command tool that this post (by scott gu) suggests is functionally equivalent to the IIS switching .vbs script files we've created, but with the drawback of no designer support. What are others experience?

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Friday, August 18, 2006 11:31 AM by Steve Fisk
This is great - just installed VS2005 and immediately this was a problem - one that I still have though is to have a custom 404 page in the old site that I want to upgrade through and I don't know whether I can achieve the same effect using the vwd server - is this possible and if so how do I do it? Thanks Steve

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Friday, August 18, 2006 4:57 PM by ScottGu

Hi Steve,

Unfortunately the VWD server doesn't support defining custom 404 rules.  Instead, to set these you'd want to use IIS.

Hope this helps,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, September 5, 2006 4:26 AM by Robin Massart
Is there a way to specify a different page other than "default.aspx" as the default page for a project. I use "index.aspx" as the default and many of my links don't specify the filename, which causes errors because it is always looking for default.aspx.

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, September 12, 2006 1:56 PM by Mike
Great post! Thought I'd offer our environment for discussion: We're upgrading from 1.1 while developing on Windows XP and deploying to Windows Server 2003. We currently use a command line batch file and WSH .vbs script to switch IIS to point at different "virtual" directories when we need to develop a different site (limitation of IIS in XP is only one website at a time).

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Thursday, September 28, 2006 2:00 PM by Shane
Hi Scott, Thanks for the post. It is relevant to what I am working on now. However, my Use Custom Server checkbox is grayed out/disabled. Any ideas why? Shane

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, October 3, 2006 10:35 PM by Lionel
hi, i got serious problems here. i run my web program by using webdev.webserver on another machine, but it couldnt work. That machine already install .Net framework 2.0 but dont hv visual studio 2005. ( i hd setup the webdev.webhost.dll into windows' assembly folder. I used the webdev.webserver to run my web program, the web page can view but it cant load the database from machine. What's wrong izzit?

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Thursday, October 5, 2006 10:48 AM by ScottGu

Hi Lionel,

I suspect the problem is caused by the account running the web-server not having security permissions to the SQL database.  Can you double-check the access permissions?

Thanks,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Wednesday, October 25, 2006 6:10 AM by The Simplest Way
Maybe because I got VS2005 with sp1 But all I did was change the web project virtual path to "\" (click properties on the web project) and I got the desired result. Is it because the sp1 ?

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Thursday, October 26, 2006 1:26 AM by ScottGu

Hi "The Simplest Way",

The good news is that SP1 now supports root "/" based projects - so this trick no longer is needed. :-)

Thanks,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Friday, October 27, 2006 1:02 AM by ScottGu

Hi Simon,

I believe you can right-click on a web-site project and configure it in the properties dialog.

Hope this helps,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, October 31, 2006 1:01 AM by ScottGu

Hi Loek,

The final SP1 release isn't out yet -- only the beta is available to download.  If you install that, though, you'll be able to go to your project's property grid and configure the virtual directory path to be "/" instead of "/appname".

Hope this helps,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 14, 2006 8:56 AM by Thomas W. Watson
Scott, How can you use this with the WAP project and edit and continue?

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 14, 2006 1:43 PM by ScottGu

Hi Thomas,

Here is a tutorial I posted that shows how to use this with WAP and edit and continue: http://weblogs.asp.net/scottgu/archive/2006/05/08/VS-2005-Web-Application-Project-V1.0-Released.aspx

Note that WAP by default supports root projects - so you don't need to use this trick for WAP.

Hope this helps,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Friday, November 17, 2006 1:50 PM by John Hoge

This is a nice trick, but the images are not visible in the VS designer unless I use <asp:image /> tags. If I use the nice, lightweight html <img /> tag I don't get designer support.

Thanks,

John

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, November 21, 2006 2:50 PM by Adrian

Suppose you don't want to do this but you still want to develop using file based development along with being able to read or display files programatically that are in the same directory as your source files how do you handle the whole virtual file structure? I thought of using

Dim strDirectory As String = System.Reflection.Assembly.GetExecutingAssembly().Location but of course the files are not there in that virtual location. Please help.

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Sunday, November 26, 2006 2:54 PM by ScottGu

Hi Adrian,

Can you provide more details on what you are trying to retrieve?  Are you looking for the root application path?  If so, you can use Request.ApplicationPath to retrieve that.

Thanks,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Saturday, December 23, 2006 11:24 AM by Imar Spaanjaars

Hi Scott,

I have used this trick to run a root based site since you posted it. However, it seems that SP1 has introduced a little problem with it. Whenever I try to run Cassini from the Tools menu, I get a message like this:

The directory 'C:\Websites\MySite"' does not exist.

Note there's an additional double quote at the end of the folder name. I think that's causing the problem.

I tried this on a number of machines with SP1 and they all have the same problem.

I know I can use the new Virtual path property in SP1 (thanks a lot for that), but I was wondering if there's a way around this problem as I often use it to open Cassini on a specific port.

I tried $(ProjectDir) with other tools (like a Windows Explorer) and they seem to run fine.

Any ideas??

Cheers,

Imar

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Wednesday, December 27, 2006 10:25 PM by ScottGu

Hi Imar,

I'm not sure why that isn't working anymore - but I'd still recommend using the new approach here: http://weblogs.asp.net/scottgu/archive/2006/12/19/tip-trick-how-to-run-a-root-site-with-the-local-web-server-using-vs-2005-sp1.aspx

It supports setting both the virtualpath as well as the port number to use - so should provide all of the support you are using today.

Thanks,

Scott

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Thursday, December 28, 2006 8:29 AM by Imar Spaanjaars

Hi Scott,

Yeah, I agree. It's a great improvement....

Cheers,

Imar

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Saturday, December 30, 2006 1:47 PM by Geoff Taylor

Hi Imar,

I'm getting the same error as you.  I think the problem is because $(ProjectDir) has a trailing slash.  If you set the 'Prompt for arguments' checkbox on the External Tools menu, you can see the command-line it uses.

If I try running the exact same command-line from a DOS prompt, I get the same error.  If I remove the trailing slash, it works just fine.

This was working before the SP1 upgrade, so it's a bit annoying that it's broken now (although I'm not sure if it's the $(ProjectDir) handling that has changed or the development web server).  I don't really want to recreate the project as a web site project, since, well, it's not a web site, so it looks like hard-coding the project directory is the way forward for me.

Hope this helps,

Geoff

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, January 2, 2007 4:08 PM by Chris Sigrist

I had the same problem after upgrading Web Developer Express to SP1.  When trying to start my custom web server I would get "such and such directory does not exist".  After fidgeting around  with the 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

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Thursday, January 4, 2007 3:23 PM by Geoff Taylor

Excellent tip - many thanks Chris.

Geoff

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Saturday, January 13, 2007 6:46 AM by Imar Spaanjaars

Yep, that worked for me as well. Thanks Chris!

Imar

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Thursday, January 18, 2007 1:50 AM by Ashwin

Thanks a lot. You have been of great help.

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Sunday, January 28, 2007 11:20 AM by Erik

Thanks alot for these tips!!! Saved me lots of pain and time!!!

Cheers from Erik / Sweden

# re: How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Tuesday, January 30, 2007 12:41 AM by ScottGu

Hi Ed,

If you are using SP1, then you'll instead want to use this tip/trick to enable root applications: http://weblogs.asp.net/scottgu/archive/2006/12/19/tip-trick-how-to-run-a-root-site-with-the-local-web-server-using-vs-2005-sp1.aspx

Alternatively, someone above posted this comment to fix the issue you are seeing:

>>> After fidgeting around  with the settings, I found that if you place a final space and a slash after in the arguments, like this: /port:8080 /path:$(ProjectDir) /

Hope this helps,

Scott