Setting up Visual Studio .NET 2003 web applications to use a virtual directory
Last modified: 5 October 2005. Any comments or suggestions - please fill in form below. Chris Cant.
Chris is available for paid-for consultation, software development or web programming - contact us using the form below
Extra tip: making Find in Files work better
Introduction
This article explains briefly how to set up a web application in Visual Studio .NET 2003 so that the source code
is not in the localhost server directory.
As far as I can tell, you cannot do this simply in the New Project wizard.
I find it useful to keep all my source on one drive - and this drive is different
from the drive that runs my localhost server. I have done the following job a few times before, and have finally decided
that it is worth while documenting.
System: Windows XP with IIS 5.1, VS.NET 2003 and ASP.NET 1.1
Example directories used
In this example, I am suggesting that you want your source files to be in this directory:
C:\Work\MyWebApp\ .
However your localhost web server is on drive D: with the web root at D:\inetpub\wwwroot\ .
If you set up an application using the New Project wizard then the source files will be in
D:\inetpub\wwwroot\MyWebApp\ which is not what you want.
Task list
- If desired, create a new Blank Solution, eg name "MyWebApp" in Location "C:\Work"
- Select New+Project... and "ASP.NET Web Application" in Visual C# Projects or Visual Basic Projects.
Set the Location to http://localhost/MyWebApp and make sure that "Add to solution" is selected.
Close VS.NET.
- Copy all the files and sub-directories in
D:\inetpub\wwwroot\MyWebApp\ to
C:\Work\MyWebApp\ .
- Start VS.NET
Right-click on "MyWebApp" in the Solution Explorer, select Properties and show "Web Settings"
- Set the Path to
C:\Work\MyWebApp\
- I have Web Access mode set to File Share.
- Click OK.
- Select "MyWebApp" in the Solution Explorer and select Edit+Remove. Click OK.
Close VS.NET
- Delete the entire directory
D:\inetpub\wwwroot\MyWebApp\
- Select Start+All programs+Administrative Tools+Internet Information Services to show the IIS manager.
On the left, drill down until you see your web site (eg Default Web Site)
Right-click on "Default Web Site" and select New+Virtual Directory...
- Click Next
- Enter an Alias of
MyWebApp and click Next
- Enter a Directory of
C:\Work\MyWebApp\ and click Next
- Leave the settings alone and click Next
- Click Finish
Close the IIS Manager
- Start VS.NET
Select File+Add Project+Existing Project... and choose filename C:\Work\MyWebApp\MyWebApp.csproj or similar
- Select File+Save All
You are now ready to work with your new project in your desired directory.
Extra tip: making Find in Files work better
In VS.NET 2003, if you use "Edit+Find and Replace+Find in Files" or "Replace in Files",
I have found that text in code for web page files is NOT found.
For example, searching for "InitializeComponent" will not be found in Global.asax.cs ;
and "Page_Load" is not found in WebForm1.aspx.cs . This is a complete pain in the neck.
My solution is keep the code associated with a web page to a minimum, ie put your main code elsewhere.
So create a new class in the project called MyWebAppGlobal.cs and derive Global
in Global.asax.cs from this, eg:
public class Global : MyWebAppGlobal
In MyWebAppGlobal.cs derive MyWebAppGlobal from System.Web.HttpApplication, eg:
public class MyWebAppGlobal : System.Web.HttpApplication
Similarly, create a class MyPage.cs . In WebForm1.aspx.cs , derive
WebForm1 from MyPage , eg:
public class WebForm1 : MyPage
Delete the entire function Page_Load() in WebForm1.aspx.cs .
In MyPage.cs add this:
protected void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
Comments:
- Daniel, Mon, 05 Dec 2005 08:43:34 (GMT)
- Thanks for the virtual directory tip.
- Devang Shah, Fri, 21 Sep 2007 13:51:07 -0700
- You need to ask one more thing in the task list. Enable 'Browsing' in virtual directory set up (right near the end)
Reply: Only enable 'Directory Browsing' if you want the list of files in the directory to be shown when the user visits the
directory.
Devang Shah: This is an important question. I suspect people develop on development machine, typically behind a firewall and then deploy on a production server. At least, that's what we would hope for a company that can even afford to purchase Microsoft development tools and operating systems, not to mention IIS license. I tried your techniques on a Windows XP machine with 'Internet Information Services' installed as a 'Windows Components'. The machine had all the service packs installed and it was a nightmare to get it going. Allowing browseability was just one of the things that I had to do.
I need to prepare a document with your checklist (I intend to properly attribute your blog) with the modifications.
One other major modification was that IE7 has a security feature that prevented me from adding the existing project too. I will send you the URL once I have the document in place.
|