1.1. Create a New Web Project
The way you create a new web application project has changed in Visual Studio 2005. In Visual Studio .NET 2003, you select File New Project on the Visual Studio menu and then select the kind of project you want to create (Web, Windows, Windows Services, and so on) in the New Project Dialog. In Visual Studio 2005, you open a new web project with the New Web Site... command in the File menu.
Visual Studio 2005 Editor Settings
When you launch Visual Studio 2005 for the first time, you will be asked to choose a default setting for the IDE. Among the list of settings available are:
Depending on which setting you choose, you will get a slightly different menu setup.
Note: With Visual Studio 2005, it's now easier than ever to start building an ASP.NET web application. You've got to try it to believe it!
1.1.1. How do I do that?
To get a feel for how you create a new web application in Visual Studio 2005, try walking through the following steps. First we'll open the application, pick a location to host it for development, and then add a second web form. Finally, we will see how we can debug the application.
You can change the IDE settings (listed in Sidebar 1-2 via Tools Import and Export Settings.... Since this book is all about web development, we naturally suggest you select Web Development Settings. Launch Visual Studio 2005. If you selected the Web Development Settings option in Visual Studio 2005, go to the File menu and select New Web Site..., as shown at the top of Figure 1-1. If you have chosen any other setting, go to the File menu and choose New Web Site... (see the bottom part of Figure 1-1).
Figure 1-1. Creating a new web site in Visual Studio 2005
When the New Web Site dialog box appears, select a project template from the Templates pane. Since we're creating a blank ASP.NET web site in this lab, select the ASP.NET Web Site template.
In the New Web Site dialog, you need to choose a development language and a place to locate the project. We'll be using Visual Basic as our language of choice throughout this book, so from the Language drop-down listbox, select Visual Basic.
Visual Studio 2005 provides four ways to develop a web site. For this lab, you'll use the File System option. Choose File System from the Location drop-down list. This new ASP.NET 2.0 option frees you from having to use IIS (Microsoft Internet Information Server) for development. Instead, Visual Studio 2005 provides its own built-in web server, which it launches when you run the web application. Use C:\ASPNET20\ as the location of your application and chap01-WebSite1 as its name. Type the complete pathname, C:\ASPNET20\chap01-WebSite1, into the drop-down combo box to the right of the Location box.
Note: No more worries about creating virtual directories on your web server! Visual Studio 2005 comes with a file-based web server for developing ASP.NET 2.0 applications. Now you can also develop ASP.NET 2.0 web applications on a Windows XP Home Edition PC, which traditionally does not include IIS.
Figure 1-2 shows the completed New Web Site dialog.
Figure 1-2. Selecting a project language, template, and location
Tip: For all subsequent examples in this book, you will use C:\ASPNET20\ as the directory in which to store your projects.
If you do wish to use IIS for development purposes, select HTTP from the Location drop-down list and enter a URL for the application instead, such as http://localhost/chap01-WebSite1.
Order of Installation
If you wish to use IIS to develop your ASP.NET web applications, you must install IIS before installing Visual Studio 2005. By default, Windows XP does not install IIS, and so you need to retrieve your Windows XP Installation CD and then use Control Panel Add or Remove Programs Add/Remove Windows Components Components: Internet Information Services (IIS) to add IIS yourself.
In the event that you have installed Visual Studio 2005 before installing IIS, you need to associate IIS with ASP.NET. You can do so by using the aspnet_regiis utility (located in C:\WINDOWS\Microsoft.NET\Framework\<version>) with the -i option, like this:
Once you have selected your template, language, and location, click OK. Visual Studio creates your project, and the Solution Explorer should display the files shown in Figure 1-3.
Figure 1-3. The Solution Explorer
By default, ASP.NET creates a folder named App_Data and an initial application page, a Web Form named Default.aspx with a code-behind page named Default.aspx.vb (to see the contents of this file, click the + symbol to the left of Default.aspx icon in the Solution Explorer).
Switching Between Design View, Source View, and Code-Behind View
In Visual Studio 2005, a Web Form is displayed in Source View by default. In Source View, you can modify the various attributes of the form and the controls contained within it. To switch to Design View, click on the Design button at the bottom of the screen. In Design View, you can visually inspect the page and drag and drop controls onto the form. To view the code-behind of the form, you can simply double-click on the form and the code-behind will appear. In Code View, you write your business logic for your application as well as service the events raised by the various controls on the page. Figure 1-4 shows the three views.
Figure 1-4. Switching Between the Different Views
To add a new item (such as an additional Web Form) to your project, you can right-click the project name and select a template from the Add New Item... dialog box shown in Figure 1-5.
Figure 1-5. Adding a new item to your web project
Notice that you have the option to "Place code in separate file." If this option is unchecked, your code will reside in the same file as your Web Form. For all the examples in this book, you will place the code in a separate file. Hence, check the "Place code in separate file" option. Click Add.
Tip: If you wish to debug your application (by using F5), you need to have a Web.config file in your project. By default, if there is no Web.config file when you try to debug your application, Visual Studio will prompt you to add one.In Visual Studio 2005 (unlike Visual Studio .NET 2003), Web.config is not automatically added to your project. To add a Web.config file yourself, simply go to the Add New Item... dialog box, and select Web Configuration File from the "Visual Studio installed templates" window.
1.1.2. What about...
...modifying the code generated by the Visual Designer?
If you look at the code-behind of a Web Form, you will realize that the bulk of the user interface code generated by Visual Studio is no longer visible, as it has been in ASP.NET 1.x. Instead, you see a partial class:
Partial Class Default_aspx End Class
You add your business logic to this partial class. Unlike ASP.NET 1.x, where the code-behind contains code generated by the Visual Designer, ASP.NET 2.0 does not display this section. Instead, at compile time, the Visual Designer automatically generates the partial class needed to implement the user interface and merges it with your code-behind.
The New Partial Keyword
One of the language enhancements in .NET 2.0available to both VB2005 and C# 2.0 programmersis support for partial classes. In a nutshell, partial classes mean that your class definition can be split into multiple physical files. Logically, partial classes do not make any difference to the compiler. During compile time, it simply groups all the various partial classes and treats them as a single entity.
One of the greatest benefits of partial classes is that they allow a clean separation of business logic and the user interface (in particular, the code that is generated by the Visual Studio Designer). Using partial classes, the UI code can be hidden from the developer, who usually has no need to access it anyway. Partial classes also make debugging easier, as the code is partitioned into separate files. This feature also helps members of large development teams work on their pieces of a project in separate physical files.
...choosing another location for your web application?
Visual Studio 2005 provides four possible locations for a web application. If you are developing a simple web application (or are just trying out some of the new features in ASP.NET), the quick and easy way to build the application is to use the File System method. This method is also useful for developers who do not have a web server (such as IIS) installed on their machine, or for developers who are using Windows XP Home Edition as their development workstation.
If you already have a web server installed on your machine and you want to use it to host your web application, you can choose the Local IIS method. Doing so allows your web application to be accessed from other machines during development time.
If your web server is located remotely, such as in a hosting environment, then you could use the FTP method. Use FTP Sites if your hosting vendor supports FTP access. Alternatively, you can host your application on another remote server through HTTP using the Remote Web Site option. To use this option, the remote server must be configured with the FrontPage Server Extensions.
Improved Debugging Support in ASP.NET 2.0
In ASP.NET 1.x, you need to explicitly set a start page in your project so that a specific page is loaded when you press F5 to debug the application. In ASP.NET 2.0, you can still set a specific page as the start page if you want. However, you can also set the start page as the currently selected page (currently selected either because you're editing it or because you selected the page in Solution Explorer). This feature saves you the trouble of setting a start page when you just want to debug a page you're working on at the moment.
This option is configurable via the Start Options item in the project Property Pages dialog (right-click a project name in Solution Explorer and then select Property Pages), as shown in see Figure 1-6.
Figure 1-6. The project Property Pages
1.1.3. Where can I learn more?
Visit the Visual Studio 2005 home page at http://lab.msdn.microsoft.com/vs2005/ for information on the latest changes to Visual Studio 2005.
If you prefer to use the Visual Web Developer 2005 Express Edition, head over to http://lab.msdn.microsoft.com/express/vwd/default.aspx for information on how to download a trial copy.