Wrox Home  
Search
Beginning ASP.NET 3.5: in C# and VB
by Imar Spaanjaars
March 2008, Paperback


Excerpt from Beginning ASP.NET 3.5: in C# and VB

Get Started with Microsoft Visual Web Developer 2008 and ASP.NET 3.5

by Imar Spaanjaars

Although you could theoretically write ASP.NET 3.5 Web applications with Notepad or another text editor alone, you really want to install a copy of Microsoft Visual Web Developer. VWD is developed specifically for building ASP.NET Web sites, and as such, hosts an enormous amount of tools that will help you in rapidly creating complex ASP.NET Web applications.

Visual Web Developer comes in two flavors: as a standalone and free version called Microsoft Visual Web Developer 2008 Express Edition, and as part of the larger development suite called Visual Studio 2008 which is also available in different editions, each with its own price tag. Although the Express Edition of VWD is free, it contains all the features and tools you need to create complex and feature-rich Web applications. Getting VWD is easy. You can just download it from the Microsoft site as discussed next.

Getting Visual Web Developer

You can get the free version of VWD from Microsoft's site at http://msdn.microsoft.com/vstudio/express/. On the Express home page, follow the Download link until you reach the page that offers the downloads for the Express products, including Visual Web Developer 2008 Express Edition. (You may need to choose the Web Development category first.) Read the system requirements to make sure VWD will run on your system and then download VWD 2008 Express Edition. Make sure you choose Visual Web Developer from the page, and not one of the other free Express products.

Don't be fooled by the file size of the download, which is a little under 3MB. The file you downloaded is just the installer that downloads the required files over the Internet. The total download is around 1.3GB.

Installing Visual Web Developer Express Edition

Installing Visual Web Developer is a straightforward, although somewhat lengthy process. Depending on your installation method, your computer and your Internet connection speed, installing VWD may take up to several hours.

Try It Out Installing Visual Web Developer 2008 Express Edition

This Try it Out exercise guides you through installing VWD Express Edition on your computer. It assumes you're using the Web download option, although the process for installing the Express edition from a DVD is almost identical. The steps you need to perform to install the full versions of Visual Studio 2008 are similar as well, although the screens you'll see will be somewhat different.

No matter which version of VWD you install, it's important that you also install SQL Server 2005 Express Edition — a required component if you want to follow along with many of the examples in the book, Beginning ASP.NET 3.5: in C# and VB (Wrox, 2008, ISBN: 978-0-470-18759-3). When you install the full version of Visual Studio 2008, the option to install SQL Server is included on the list with features to install that you see during setup. If you install VWD Express Edition, you get the option to choose SQL Server on the Installer Options dialog. If you don't see SQL Server listed on these dialogs, you probably already have SQL Server 2005 Express Edition installed.

  1. When you're installing the Web version, run the file you downloaded from the Microsoft Web site. Otherwise, start the setup process from the Visual Studio or Visual Web Developer DVD.
  2. Once the installer has started, click Next, read and accept the license terms, and click Next once more.
  3. On the Installer Options page, make sure you select both the MSDN Express Library for Visual Studio 2008 and Microsoft SQL Server 2005 Express Edition. Although these two options add considerably to the size of the download, both of them are invaluable for building ASP.NET Web applications. If you don't see the SQL Server option, you already have it installed. Click Next again.
  4. On the Destination Folder page, you can leave the Install in folder field set to its default if you have enough space on your primary disk. Otherwise, click the Browse button and select a different location.
  5. Click the Install button. If you're using the Web-based installer, the setup application will first download the files over the Internet to your computer. During the installation process, you'll see a screen (similar to that in Figure 1) that shows you the progress of the download and installation of VWD.
Figure 1
Figure 1
  1. Once the application is finished installing, you may get a dialog asking to reboot your machine. Click Restart now. Once your machine has started again, VWD is ready for use.

Creating Your First ASP.NET 3.5 Web Site

You probably can't wait to get started with your first ASP.NET Web site, so instead of giving you a theoretical overview of Web sites in VWD, the next Try It Out dives right into the action and shows you how to build your first Web project. Afterward, in the How It Works explanation and the section that follows, you get a good look of what goes on behind the scenes when you view an ASP.NET page in your browser.

Try It Out Creating Your First ASP.NET Web Page
  1. Start VWD from the Windows Start menu if you hadn't already done so. The first time you start VWD, there is a long delay before you can use VWD as it's busy configuring itself. Subsequent starts of the application will go much faster.
  2. If you're using a commercial version of Visual Studio, you also get a dialog box that lets you choose between different collections of settings the first time you start Visual Studio. The choice you make on that dialog influences the layout of windows, toolboxes, menus, and shortcuts. Choose Web Development Settings because those settings are designed specifically for ASP.NET developers. You can always choose a different profile later by resetting your settings.
  3. Once VWD is fully configured, you see the main screen appear, as shown in Figure 2.
Figure 2
Figure 2
You get a full description of all the windows, toolbars, panels, and menus in the next section, so for now, just focus on creating a new Web site. Click the File menu in the upper-left corner and choose New Web Site. If you're using a commercial version of Visual Studio, you may have to open the submenu New first. (Make sure you don't accidentally use the New Project menu, as that is used to create different types of .NET applications.) The New Web Site dialog appears as shown in Figure 3.
Figure 3
Figure 3
  1. In the Templates section of the dialog, make sure that ASP.NET Web Site is selected. Also make sure that File System is the selected option in the Location drop down. If you want, you could change the location on disk where the Web site is stored by clicking the Browse button and choosing a new location on your computer's hard drive. For now, the default location — a folder under your Documents folder — is fine, so you can leave the location as is.
  2. In the Language drop down, you can choose a programming language you will use mainly in your site. Choose a language to your liking.
  3. Click OK. VWD creates a new Web site for you that includes one standard ASP.NET page called Default.aspx, a Web.config file, and an empty App_Data folder, as shown in Figure 4. It also opens the file Default.aspx so you can see the code for the page.
Figure 4
Figure 4
  1. Between the opening and closing <div> tags in the page, type the highlighted text and code:
<div>
  <h1>Hello World</h1>
  <p>Welcome to Beginning ASP.NET 3.5 on <%= DateTime.Now.ToString() %></p>
</div>
  1. From the Debug menu in VWD, choose Start Without Debugging (or press Ctrl+F5) to open the page in your default browser, as shown in Figure 5.
Figure 5
Figure 5
If you don't see the date and time in the page, or if you get an error, look again at the code in the welcome message. It starts with an angle bracket (<) followed by a percentage symbol and an equals sign. It closes with a single percentage sign and another angle bracket (>). Also, make sure you typed in the code exactly as shown here, including capitalization. This is especially true when you are using C#, as that language is case sensitive.

If you get an Information bar warning about Intranet settings in Internet Explorer, click the bar and choose Enable Intranet Settings. If you want to learn more about the implications of these settings first, choose What are Intranet Settings from the popup menu.

  1. Notice how a little icon with a screen tip appeared in the tray bar of Windows, visible in Figure 6. This icon belongs to the ASP.NET Development Server. This Web server has been started by VWD automatically to serve the request for your page.
Figure 6
Figure 6

That's it. You just created your very first ASP.NET 3.5 Web site with VWD.

However, there's more to the New Web Site dialog than you saw in the previous example. You may have noticed that you can choose from a number of different templates that allow you to create different kind of sites. But before you look at the different templates on which you can base your new Web site, you need to know a little more about the different project types that are available in VWD.

Different Project Types

Depending on the version of VWD you're using, you may have the choice between two different project types. If you're using Visual Web Developer Express Edition you only have one option: Web Site Projects. If you're using one of the commercial versions of Visual Studio 2008, you also have the option to create a new Web Application Project. Both project types are discussed next.

Web Site Projects

Web Site Projects represent a project in VWD for a Web site. You create a new Web Site Project by choosing File-->New Web Site or File-->New-->Web Site from Visual Web Developer's main menu.

Web Site Projects were introduced in Visual Studio 2005 and provide some new flexibility in creating and working with Web sites. In contrast to Web sites built with earlier versions of Visual Studio .NET, a Web Site Project site is simply a Windows folder with a bunch of files and subfolders in it. There is no collective file (known as the "project file" with a .vbproj or .csproj extension) that keeps track of all the individual files in the Web site. You just point VWD to a folder, and it instantly opens it as a Web site. This makes it very easy to create copies of the site, move them, or even share them with others, as there are no dependencies with files on your local system. Because of the lack of a central project file, Web Site Projects are usually simply referred to as Web Sites.

Besides a lot of positive feedback on this move, Microsoft also received a lot of negative response from developers that complained that Web Site Projects were too limiting for their development environment. Since there is no container file that keeps track of everything in the site, it became much harder to exclude files or folders from the site and work with source control systems — a centralized system that allows developers to work on a project collaboratively and that keeps track of changes in the project automatically. Also, Web Site Projects influenced the way Web sites are compiled and deployed, making it harder for developers accustomed to the previous model to apply their knowledge and skills to the new project type.

In response to the criticism, Microsoft released the Web Application Projects in May 2006 as an add-on for Visual Studio 2005 standard edition and up. Unfortunately, Web Application Projects are not available for the Visual Web Developer Express Editions, including the new Visual Web Developer 2008. So, if you are working with the free version, you don't have much to choose; you can only use Web Site Projects.

Web Application Projects

With the new Visual Studio 2008 release, Web Application Projects are now fully integrated in the IDE. This makes it easier for developers who work in teams or who need more control over the contents of the site and their compilation and deployment processes to build Web sites with Visual Studio.

In Visual Studio 2008, you create a new Web Application Project through the File-->New Project dialog. In that dialog, expand your preferred programming language (either Visual Basic or Visual C#) and then click the Web category, where you'll find a number of ASP.NET Web application templates.

Now that you know about the different project types, the next thing to consider is the different Web site templates and their options.

Choosing the Right Web Site Template

The New Web Site dialog in VWD contains different Web site templates, each one serving a distinct purpose.

Figure 7 shows the New Web Site dialog in VWD. You can open this dialog by choosing File-->New Web Site or File-->New-->Web Site, depending on your version of VWD.

Figure 71
Figure 7

The top section of the Templates area shows the ASP.NET Web site templates that are installed by default. Each of them is discussed in the next section. The second part, labeled My Templates, contains a link to search for templates online. In addition, when you have created your own templates, or have templates installed from other parties, they show up in this area as well.

ASP.NET Web Site

This template allows you to set up a basic ASP.NET Web site. It contains a simple Web.config file (an ASP.NET configuration file), one Web Form (called Default.aspx), its Code Behind file, and an empty App_Data folder.

This template is a good starting point for all your ASP.NET Web sites.

ASP.NET Web Service

The ASP.NET Web Service template is the starting point for new sites that contain Web services. A Web service allows you to create software on the Web server that can be called by other applications located on the same machine, or on computers somewhere in the network or on the Internet. Once you create a site based on this template, you get a Web service file, an additional code file named after the service, and a Web.config file that contains configuration information accessible to the Web services.

WCF Service

The WCF Service template is somewhat similar to the Web Service template in that it allows you to create a Web site that contains services that are callable over a network. However, Windows Communication Foundation Services go much further than simple Web services and offer you a lot more flexibility. If you want to learn more about WCF Web services, pick up the book, Professional WCF Programming: .NET Development with the Windows Communication Foundation, by Scott Klein (Wrox, 2007, ISBN: 978-0-470-08984-2).

Empty Web Site

The empty Web site template gives you exactly what its name implies: nothing. All you get is an empty Web site as a starting point. The empty Web site template is useful if you have a bunch of existing files you want to use to create a new Web site.

Although it seems you have to make a clear choice up front for the right Web site template, this isn't really the case. Since an ASP.NET Web site in VWD is essentially just a reference to a folder, it's easy to add types from one template to another. For example, it's perfectly acceptable (and very common) to add a Web service file to a standard ASP.NET Web site.

This article is excerpted from Chapters 1 and 2 of Beginning ASP.NET 3.5: in C# and VB (Wrox, 2008, ISBN: 978-0-470-18759-3), by Imar Spaanjaars. Imar is technical director and software designer for Design IT, an IT company in the Netherlands specializing in Internet and intranet applications built with Microsoft technologies. In addition to extensive ASP.NET writing on his blog and co-authoring a previous Wrox ASP.NET book, he is most well-known among the 500,000 monthly developers at p2p.wrox.com for his more than 7,000 posts in the Wrox p2p.wrox.com reader discussion forums. He is by far the most well-known Wrox author and participant in this active Wrox discussion area. His answers in the forums have earned him extensive reader praise.