O'Reilly logo

Visual C# 2005: A Developer's Notebook by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4. Web Applications

The ASP.NET team at Microsoft set an ambitious goal for ASP.NET 2.0: to make it possible for web-site developers to build ASP.NET applications with 75% less coding. Their success means you will write a lot less plumbing code when you build ASP.NET 2.0-based applications, so that instead you can focus on making your web applications accessible and secure for your customers and clients. Among the improvements you'll find in ASP.NET 2.0 are powerful new controls, including data access controls that free you from repetitive database programming, and security controls that enable you to better manage user accounts and passwords. Also available are new controls for managing access to site data and for offering a uniform user experience across your web site's pages.

All of these features are available to C# programmers in all editions of Visual Studio 2005, with the exception of Visual C# 2005 Express Edition. Best of all, you no longer need Internet Information Server (IIS) to develop an ASP.NET application, a feature we'll investigate in the first lab.

Develop Web Apps Without IIS

With Visual Studio 2005 you no longer need to install and use IIS to host your web applications while you develop them. Now Visual Studio gives you four choices:

File System Web Site

This is the simplest alternative; just put all your files in a filesystem folder on your hard drive.

Local IIS Web Site

This works just like it did with ASP.NET 1.x. You can use Visual Studio 2005 to create web applications that reside either on the local IIS root or in a virtual directory.


Visual Studio 2005 gives you four ways to manage web applications.

Remote Web Site

You can use Visual Studio 2005 to create applications that reside on remote servers as long as those servers support FrontPage 2000 Server Extensions.

FTP Web Site

Visual Studio 2005 allows you to create and maintain web sites on an FTP server.

How do I do that?

The first option, a filesystem-based web site, not only is completely new with ASP.NET 2.0, but also is one of the easiest ways to create a web application. You're likely to use it often. Let's explore that option here. To get started, open Visual Studio 2005 and choose File New Web Site..., as shown in Figure 4-1.

Creating a new web site in Visual Studio 2005

Figure 4-1. Creating a new web site in Visual Studio 2005

This menu choice opens the New Web Site dialog shown in Figure 4-2. Select ASP.NET Web Site in the Templates pane of the dialog. In the Location box choose File System. In the Language box choose Visual C# (this is, after all, a book on C#). Then in the box to the right of the Location box, choose a folder to house your ASP.NET files.

The New Web Site dialog

Figure 4-2. The New Web Site dialog

Click OK. You are placed into the Default.aspx file, in Source view. Select Design view instead.


Note two changes from Visual Studio 2003. Now the default is to put you in Source view rather than Design view, and within Design view the default is a flow layout rather than a grid layout. Thus, if you want absolute positioning, you must switch to grid layout. For this book we will follow the tradition of most web programmers and use tables for laying out pages when specific placement is needed.

Drag a label control and a button control from the Toolbox onto the form and double-click the button to open the code-behind file for the page. Notice (as noted in Chapter 1) that Visual Studio 2005 web forms use partial classes by default, so all the supporting code is not made visible to you.

In the Button1_Click event handler, enter this code:

Label1.Text = "Hello.";

Try to run the application in debug mode. Visual Studio 2005 will interrupt to point out that you need a Web.config file with debugging enabled, as shown in Figure 4-3.

The Debugging Not Enabled dialog

Figure 4-3. The Debugging Not Enabled dialog

The default choice is to allow Visual Studio 2005 to create a Web.config file for you. Click OK.

What just happened?

You'll notice that a lot of things happened at once. You'll see a notification in your taskbar that Visual Studio 2005 has started the Visual Web Developer Web Server, as shown in Figure 4-4.

Visual Web Developer Web Server started

Figure 4-4. Visual Web Developer Web Server started


Visual Studio 2005 starts an instance of Visual Web Developer Web Server to test your application. Unlike IIS, which includes a web server, FTP server, SMTP server, and other facilities, the Visual Web Developer Web Server is used only to test web applications during their development.

Clicking your web form button will cause a post back to the Visual Web Developer Web Server and an update of the button label, just as if this application were being served by IIS. A quick look at the Solution Explorer will also confirm that Visual Studio has added the Web.config file to your project files.

What about...

...editing the Web.config file that Visual Studio creates for me? Can I do that?

You can edit the file by hand, but it is much easier to edit it using the new Web Site Administration Tool (WAT).

Access the WAT by opening a browser and navigating directly to your .axd file (for example, http://localhost/TestWebApplication/Webadmin.axd) or, more easily, from Visual Studio 2005. To open the WAT from within Visual Studio 2005 choose Website ASP.NET Configuration. The WAT for your application will open in a browser with four tabs, as shown in Figure 4-5.

The Web Site Administration Tool page

Figure 4-5. The Web Site Administration Tool page

Click the Application tab for links to pages that will allow you to create Web.config settings painlessly and accurately, as shown in Figure 4-6.

Configuring web settings

Figure 4-6. Configuring web settings


The Web Site Administration Tool page shown in Figure 4-5 is actually an ASP.NET application. You'll find the source code on your hard drive at c:\[WindowsDirectory]\Microsoft.NET\Framework\ [Version] \ ASP.NETWebAdminFiles.

Look closely at the URL displayed when you choose Website ASP.NET Configuration. It will show you exactly which file it is displaying (e.g., [...] /appConfig/DebugandTrace.aspx).

...what about deploying a file-based system?

Just copy the contents of your folder to a virtual folder on the server, and you're all set.

Where can I learn more?

The Web Site Administration Tool is reviewed in great detail in the MSDN Help files under the article "Web Site Administration Tool Overview." Also, if you do a Google search for "ASP.NET Web Site Administration Tool" you will get literally thousands of hits, many of which will lead to articles on how to get the most out of this useful utility.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required