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

48 Comments

  • >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.

  • 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.

  • This is awesome! Thanks.

  • >>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

  • 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 : "maybe didn't have access to all of this material" : Scott only published it yesterday! :-)



    This is a neat trick, indeed !





    Thanks, Scott!

  • 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!

  • 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

  • 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 ;)

  • Please ignore my last post... I had a javascript error... sorry to bother you!

  • 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 :-(

  • 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?).

  • For some reason the 'Use custom server' option is disabled for me. Any idea what might be going on there?

  • 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

  • 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

  • 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.

  • 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.

  • Vielen Dank für diesen interessanten Artikel!!

    Many Thanks for you - for this articel!!

  • 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.

  • 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

  • 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 :)

  • 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!

  • 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?

  • 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

  • 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

  • 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.

  • 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).

  • 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

  • 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?


  • 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

  • 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 ?

  • Hi "The Simplest Way",

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

    Thanks,

    Scott

  • Hi Simon,

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

    Hope this helps,

    Scott

  • 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

  • Scott, How can you use this with the WAP project and edit and continue?

  • 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

  • This is a nice trick, but the images are not visible in the VS designer unless I use tags. If I use the nice, lightweight html tag I don't get designer support.

    Thanks,
    John

  • 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.

  • 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

  • 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

  • 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

  • Hi Scott,

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

    Cheers,

    Imar

  • 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

  • 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

  • Excellent tip - many thanks Chris.

    Geoff

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

    Imar

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

    Cheers from Erik / Sweden

  • 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

Comments have been disabled for this content.