32.2. Creating Web Projects

In addition to the standard ASP.NET Web Application and Web Site projects, Visual Studio 2008 provides support and templates for several specialized web application scenarios. These include web services, WCF services, server control libraries, and reporting applications. However, before we discuss these you should understand how to create the standard project types.

32.2.1. Creating a Web Site Project

As mentioned previously, creating a Web Site project in Visual Studio 2008 is slightly different from creating a regular Windows-type project. With normal Windows applications and services, you pick the type of project, name the solution, and click "OK". Each language has its own set of project templates and you have no real options when you create the project. Web Site project development is different because you can create the development project in different locations, from the local file system to a variety of FTP and HTTP locations that are defined in your system setup, including the local IIS server or remote FTP folders.

Because of this major difference in creating these projects, Microsoft has separated out the Web Site project templates into their own command and dialog. Selecting New Web Site from the File New sub-menu will display the New Web Site dialog, where you can choose the type of project template you want to use (see Figure 32-1).

Figure 32.1. Figure 32-1

Most likely, you'll select the ASP.NET Web Site project template. This creates a web site populated with a single default web form and a basic web.config file that will get you up and running quickly. The Empty Web Site project template creates nothing more than an empty folder and a reference in a solution file. The remaining templates, which are for the most part variations on the Web Site template, are discussed later in this chapter. Regardless of which type of web project you're creating, the lower section of the dialog enables you to choose where to create the project as well as what language should be used as a base for the project.

The more important choice you have to make is where the web project will be created. By default, Visual Studio expects you to develop the web site or service locally, using the normal file system. The default location is under the Documents/Visual Studio 2008/WebSites folder for the current user, but you can change this by overtyping the value, selecting an alternative location from the drop-down list, or clicking the "Browse" button.

The Location drop-down list also contains HTTP and FTP as options. Selecting HTTP or FTP will change the value in the filename textbox to a blank http:// or ftp:// prefix ready for you to type in the destination URL. You can either type in a valid location or click the "Browse" button to change the intended location of the project.

The Choose Location dialog (shown in Figure 32-2) enables you to specify where the project should be stored. Note that this isn't necessarily where the project will be deployed, as you can specify a different destination for that when you're ready to ship, so don't expect that you are specifying the ultimate destination here.

Figure 32.2. Figure 32-2

The File System option enables you to browse through the folder structure known to the system, including the My Network Places folders, and gives you the option to create sub-folders where you need them. This is the easiest way of specifying where you want the web project files, and the way that makes the files easiest to locate later.

Although you can specify where to create the project files, by default the solution file will be created in a new folder under the Documents/Visual Studio 2008/Projects folder for the current user. You can move the solution file to a folder of your choice without affecting the projects.

If you are using a local IIS server to debug your Web Site project, you can select the File System option and browse to your wwwroot folder to create the web site. However, a much better option is to use the local IIS location type and drill down to your preferred location under the Default Web Site folders. This interface enables you to browse virtual directory entries that point to web sites that are not physically located within the wwwroot folder structure, but are actually aliases to elsewhere in the file system or network. You can create your application in a new web application folder or create a new virtual directory entry in which you browse to the physical file location and specify an alias to appear in the web site list.

The FTP site location type is shown in Figure 32-2, which gives you the option to log into a remote FTP site anonymously or with a specified user. When you click "Open", Visual Studio saves the FTP settings for when you create the project, so be aware that it won't test whether the settings are correct until it attempts to create the project files and save them to the specified destination.

You can save your project files to any FTP server to which you have access, even if that FTP site doesn't have .NET installed. However, you will not be able to run the files without .NET, so you will only be able to use such a site as a file store.

The last location type is a remote site, which enables you to connect to a remote server that has FrontPage extensions installed on it. If you have such a site, you can simply specify where you want the new project to be saved, and Visual Studio 2008 will confirm that it can create the folder through the FrontPage extensions.

Once you've chosen the intended location for your project, clicking "OK" tells Visual Studio 2008 to create the project files and propagate them to the desired location. After the web application has finished initializing, Visual Studio opens the Default.aspx page and populates the Toolbox with the components available to you for web development.

The Web Site project has only a small subset of the project configuration options available under the property pages of other project types, as shown in Figure 32-3. To access these options right-click the project and select Property Pages.

Figure 32.3. Figure 32-3

The References property page, shown in Figure 32-3, enables you to define references to external assemblies or web services. If you add a reference to an assembly that is not in the Global Application Cache (GAC), the assembly is copied to the in folder of your web project along with a .refresh file, which is a small text file that contains the path to the original location of the assembly. Every time the web site is built, Visual Studio will compare the current version of the assembly in the in folder with the version in the original location and, if necessary, update it. If you have a large number of external references, this can slow the compile time considerably. Therefore, it is recommended that you delete the associated .refresh file for any assembly references that are unlikely to change frequently.

The Build, Accessibility, and Start Options property pages provide some control over how the web site is built and launched during debugging. The accessibility validation options are discussed later in this chapter and the rest of the settings on those property pages are reasonably self-explanatory.

The MSBuild Options property page provides a couple of interesting advanced options for web applications. If you uncheck the "Allow this precompiled site to be updatable" option, all the content of the .aspx and .ascx pages is compiled into the assembly along with the code-behind. This can be useful if you want to protect the user interface of a web site from being modified. Finally, the "Use fixed naming and single page assemblies" option specifies that each page be compiled into a separate assembly rather than the default, which is an assembly per folder.

32.2.2. Creating a Web Application Project

Creating a Web Application project with Visual Studio 2008 is much the same as creating any other project type. Select File New Project and you will be presented with the New Project dialog box, shown in Figure 32-4. By filtering the project types by language and then by using Web, you will be given a selection of templates that is partially similar to those available for Web Site projects.

Figure 32.4. Figure 32-4

The notable difference in available project templates is that the empty site and reporting templates are not available as Web Application projects. However, the Web Application project type includes templates for creating several different types of server controls.

Once you click "OK" your new Web Application project will be created with a few more items than the Web Site projects. It includes an AssemblyInfo file, a References folder, and a My Project item under the Visual Basic or Properties node under C#.

You can view the project properties pages for a Web Application project by double-clicking the Properties or My Project item. The property pages include an additional Web page, as shown in Figure 32-5.

Figure 32.5. Figure 32-5

The options on the Web page are all related to debugging an ASP.NET web application and are covered in Chapter 45, "Advanced Debugging Techniques."

32.2.3. Other Web Projects

In addition to the standard ASP.NET Web Site and Web Application project templates, there are templates that provide solutions for more specific scenarios.

  • ASP.NET Web Service: This creates a default web service called Service.asmx, which contains a sample Web method. This is available for both Web Site and Web Application projects.

  • WCF Service: This creates a new Windows Communication Foundation (WCF) service, which contains a sample service endpoint. This is available for both Web Site and Web Application projects.

  • Reporting Web Site: This creates an ASP.NET web site with a report (.rdlc) and a ReportViewer control bound to the report. This is only available as a Web Site project.

  • Crystal Reports Web Site: This creates an ASP.NET web site with a sample Crystal Report. This is only available as a Web Site project.

  • ASP.NET Server Control: Server controls include standard elements such as buttons and textboxes, and also special-purpose controls such as a calendar, menus, and a treeview control. This is only available as a Web Application project.

  • ASP.NET AJAX Server Control: This contains the ASP.NET web server controls that enable you to add AJAX functionality to an ASP.NET web page. This is only available as a Web Application project.

  • ASP.NET AJAX Server Control Extender: ASP.NET AJAX extender controls improve the client-side behavior and capabilities of standard ASP.NET web server controls. This is only available as a Web Application project.

There are further project templates available through add-on downloads. Good examples are the ASP.NET MVC and Silverlight 2.0 project types, which are discussed in Chapter 37.

32.2.4. Starter Kits, Community Projects, and Open-Source Applications

One of the best ways to learn any new development technology is to review a sample application. The Microsoft ASP.NET web site contains a list of starter kits and community projects at http://www.asp.net/community/projects. These web applications are excellent reference implementations for demonstrating best practices and good use of ASP.NET components and design.

At the time of writing, the starter kits had not been updated to version 3.5 of the .NET Framework. However, they are still very useful as they demonstrate a wide range of more advanced ASP.NET technologies and techniques including multiple CSS themes, master-detail pages, and user management.

The Microsoft ASP.NET site also contains a list of popular open-source projects that have been built on ASP.NET. By far the most up-to-date and comprehensive is the DinnerNow.net sample application, available at http://www.dinnernow.net/. Although it is categorized as an open-source application, it is really a reference implementation of many of the latest technologies from Microsoft.

The DinnerNow.net application is a fictitious marketplace where customers can order food from local restaurants for delivery to their homes or offices. In addition to the latest ASP.NET components, it demonstrates the use of IIS7, ASP.NET AJAX Extensions, LINQ, Windows Communication Foundation, Windows Workflow Foundation, Windows Presentation Foundation, Windows Powershell, and the .NET Compact Framework.

Another great place to find a large number of excellent open-source examples is CodePlex, Microsoft's open-source project-hosting web site. Located at http://www.codeplex.com/, CodePlex is a veritable wellspring of the good, the bad, and the ugly in Microsoft open-source applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.228