Article
ASP.NET 2.0: A Getting Started Guide
Virtual Directories
As we saw in the section called "Where do I Put my Files?", physical sub-folders of C:\Inetpub\wwwroot also become subdirectories of the web site. For instance, imagine your company has a web server that serves documents from C:\Inetpub\wwwroot. Your users can access these documents through http://www.example.com/. If you create a subfolder of wwwroot, named about, files in that directory can be accessed via http://www.example.com/about/.
You could also set up another subdirectory in your web site, but serve files from a different location on the disk. If, for instance, you were developing another web application, you could store the files for it in C:\dev\OrderSystem. You could then create within IIS a new virtual directory called, say, order, which mapped to this location. This new site would then be accessible through the URL http://www.example.com/order/. As this application is in development, you would probably want to set IIS to hide this virtual directory from the public until the project is complete; your existing web site would still be visible.
By default, a virtual directory, called IISHelp, is preconfigured in IIS; it maps to c:\windows\help\iishelp. You can see in Figure 1.7 that IISHelp contains subdirectories called common and iis--these are physical folders inside c:\windows\help\iishelp.

Figure 1.7. The IISHelp virtual directory
Let's create a virtual directory on your server, and test it with a simple page:
First, you need to create on your disk a folder to which your virtual directory will be mapped. Create a folder called WebDocs in an easily accessible location on your disk, such as C:\, and create a folder named Learning inside that folder. We'll use this folder, C:\WebDocs\Learning, for various exercises in this book.
Copy the index.htm file you created earlier into your newly created Learning folder.
In the Internet Information Services management console, right-click Default Web Site and select New > Virtual Directory. The Virtual Directory Creation Wizard will appear. Click Next.
You need to choose an alias for your virtual directory: enter Learning, then click Next.
Browse and select the Learning folder you created at step 1, or enter its full path (C:\WebDocs\Learning). Click Next.
In the next screen, you can select permissions settings for your directory. Typically, you'll want to leave the default options (Read and Run scripts) checked. Click Next.
Click Finish. You'll see your new virtual directory as a child of Default Web Site, as Figure 1.8 illustrates.

Figure 1.8. Creating a new virtual directory
Load this link by entering http://localhost/Learning/index.htm into the address bar of your browser. If everything went well, you should see your little HTML page load, as has the one in Figure 1.9.

Figure 1.9. Testing your new virtual directory
Note that by loading the page through the HTTP protocol, your request goes through IIS. Since index.htm is a simple HTML page that doesn't need any server-side processing, you can also load it directly from the disk. However, this won't be the case with the ASP.NET scripts you'll see through the rest of this book.
Once your new virtual directory has been created, you can see and configure it through the Internet Information Services management console shown in Figure 1.8. You can see the folder's contents in the right-hand panel.
As index.htm is one of the default document names, you can access that page just by entering http://localhost/Learning/ into your browser's address bar. To see and edit the default document names for a virtual directory (or any directory, for that matter), you can right-click the directory's name in the IIS management console, click Properties, and select the Documents tab. You'll see the dialog displayed in Figure 1.10.

Figure 1.10. Default document types for the Learning virtual directory
By default, when we request a directory without specifying a filename, IIS looks for a page with the name of one of the default documents, such as index.htm or default.htm. If there is no index page, IIS assumes we want to see the contents of the requested location. This operation is allowed only if the Directory Browsing option is selected for the directory in question. You'll find that option in the Directory tab of the Properties window.
Directory Browsing
Enabling directory browsing is not something you'd usually want to do. Allowing visitors to freely see and access all the files and directories that make up your web page is not only a little messy and unprofessional, but also increases the potential for security issues (you don't want any hackers to stick their nose into your code, do you?). So, by default, IIS won't allow directory browsing when a directory is requested: if a default file such as index.htm isn't there, ready to be served to the visitor, a message reading "Directory Listing Denied" will be served instead.
To change your virtual directory's options, you have to right-click the virtual directory (Learning, in our case) in the IIS console, and choose Properties. The Properties dialog that we've just used lets us configure various useful properties, including:
- Virtual Directory - This option allows you to configure directory-level properties, including path information, the virtual directory name, access permissions, etc. Everything that was set up through the wizard is modifiable through this tab.
- Documents - This option allows you to configure a default page that displays when the user types in a full URL. For instance, because
default.aspxis listed as a default page, the user need only enterhttp://www.mysite.com/, rather thanhttp://www.mysite.com/default.aspx, into the browser's address bar. You can easily change and remove these default pages by selecting the appropriate button to the right of the menu. - Directory Security - This option provides you with security configuration settings for the virtual directory.
- HTTP Headers - This option gives you the ability to forcefully control page caching on the server, add custom HTTP Headers, Edit Ratings (this helps identify the content your site provides to users), and create MIME types. Don't worry about this for now.
- Custom Errors - This option allows you to define your own custom error pages. Rather than presenting the standard error messages that appear within Internet Explorer, you can customize error messages with your company's logo and messages of your choice.
- ASP.NET - This tab allows you to configure the options for the ASP.NET applications stored in that folder.
One thing to note at this point is that we can set properties for the Default Web Site node, and choose to have them "propagate" down to all the virtual directories we've created.
Using Cassini
If you're stuck using a version of Windows that doesn't support IIS, you'll need to make use of Cassini to get your simple ASP.NET web applications up and running. Cassini doesn't support virtual directories, security settings, or any of IIS's other fancy features; it's just a very simple web server that gives you the basics you need to get up and running.
To get started using Cassini:
- Create a directory called
C:\WebDocs\Learning, just like the one we created in the section called "Virtual Directories". - Copy index.htm into this folder. We first saw index.htm in the section called "Using localhost".
- Start Cassini by opening
C:\Cassini(or, if you chose to install Cassini somewhere else, open that folder), then double-click on the fileCassiniWebServer.exe. - Cassini has just three configuration options:
- Application Directory - It's here that your application's files are stored. Enter C:\WebDocs\Learning into this field.
- Server Port - Web servers almost always operate on port 80, so we won't touch this setting.
- Virtual Root - This is similar to IIS's virtual directories feature, though it's nowhere near as flexible. By default, it's set to
/, meaning that you can access the fileC:\WebDocs\Learning\index.htmby entering the addresshttp://localhost/index.htm. However, to match our IIS virtual directory configuration, we want to make this file's addresshttp://localhost/Learning/index.htm. To create this effect, enter/Learning/into this field.
- Once you have filled in the Application Directory and Virtual Root fields, click the Start button to start the web server.
- After clicking Start, a link to the web site that's being served will appear toward the bottom of the window, as shown in Figure 1.11.

Figure 1.11. Cassini serving a web site
When you browse open this site in Cassini, you won't see index.htm straight away--you'll be presented with a list of files instead. Cassini only recognizes files named default.htm or default.aspx as default documents, and it doesn't allow you to configure this feature as IIS does.