Lesson 1: Building a Web Application
At its simplest level, building an application in Visual Studio .NET requires almost no effort. In fact, the application is built automatically any time you click Run while developing or debugging the application.
However, when you are ready to deploy your application, you need to take these additional steps before building it:
Set the build options.
Identify the application.
Configure the application.
In this lesson, you ll learn how to perform these tasks to prepare your Web application before deployment.
After this lesson, you will be able to
Build a release version of a Web application
Add name, company, copyright, description, and version information to the application s assembly
Set the application s configuration options in its Web.config file
Estimated lesson time: 10 minutes
Setting Build Options
Web applications have two build options: debug and release.
To change the build options, follow these steps:
Select the project in Solution Explorer, and then select the build option from the Solution Configurations drop-down list in the Visual Studio toolbar, as shown in Figure 9-1.
Figure 9-1. Selecting the build configuration in the toolbar
From the Project menu, select Properties. Visual Studio displays the project Properties Pages dialog box, as shown in Figures 9-2 and 9-3.
Figure 9-2. Project Property Pages dialog box (Visual Basic .NET)
Figure 9-3. Project Property Pages dialog box (Visual C#)
The Common Properties and Configuration Properties folders in the project Property Pages dialog box allow you to set the build properties used when compiling the project. These options are found in different places for Microsoft Visual Basic .NET and Microsoft Visual C# projects, as described in Tables 9-1 and 9-2.
Task | Folder and item | Set to |
Make the project compatible with an earlier Microsoft .NET Framework version (Visual Studio version 1.1 and later). | Common Properties, Build, Supported Runtimes | On the Build page, click Change to open the .NET Framework Version dialog box, and select support for earlier versions of the .NET Framework, such as 1.0.3075. |
Optimize the compiled code. | Configuration Properties, Optimizations | Select the Enable Optimizations check box to make the compiled code smaller, faster, and more efficient. Selecting this option makes debugging more difficult, however. |
Disable integer overflow checks. | Configuration Properties, Optimizations | Select the Remove Integer Overflow Checks check box to perform integer calculations without checking for division by zero or overflow conditions. This is faster at run time but prevents these errors from being raised at run time. |
Allow classes to be used from the Component Object Model (COM). | Configuration Properties, Build | Select the Register For COM Interop check box to generate a type library and register the application in the system registry. This option has an effect only on Class Library project types. |
Task | Folder and item | Set to |
Make the project compatible with an earlier .NET Framework version (Visual Studio version 1.1 and later). | Common Properties, General, Applicaton, Supported Runtimes | Click the ellipsis ( ) button to open the .NET Framework Version dialog box, and select support for earlier versions of the .NET Framework, such as 1.0.3075. |
Optimize the compiled code. | Configuration Properties, Build, Code Generation, Optimize Code | Select True to make the compiled code smaller, faster, and more efficient. Selecting True makes debugging more difficult, however. |
Disable integer overflow checks. | Configuration Properties, Build, Code Generation, Check For Arithmetic Overflow/Underflow | Select False to perform integer calculations without checking for division by zero or overflow conditions. This is faster but prevents these errors from being raised at run time. |
Allow the use of the unsafe keyword. | Configuration Properties, Build, Code Generation, Allow Unsafe Code Blocks | Select True to allow the project to use pointers within procedures defined with the unsafe keyword. |
Allow classes to be used from COM. | Configuration Properties, Build, Outputs, Register For COM Interop | Select True to generate a type library and register the application in the system registry. This option affects only Class Library project types. It is disabled for Web applications. |
Open the project s Web.config file and set the compilation element to match the build setting you selected in step 1. For example, use the following setting for a release build:
<compilation defaultLanguage="vb" debug="false" />
Selecting the debug build option generates a program database file (.pdb) containing information about symbols used within the application when the project is compiled. Visual Studio .NET uses the program database to monitor the values of variables, set breakpoints, and evaluate Debug class members.
Selecting the release build option does not generate this file; this disables Debug class members and causes breakpoints to be ignored. Because release builds don t have to monitor this extra information, they execute faster than debug builds.
The application s build option and Web.config setting should agree. There is no advantage in having one set to debug and the other set to release; however, Visual Studio .NET does not automatically change one when you change the other.
Identifying the Application
Before you deploy an application, you should include information about the application such as the application s title, author, and version. This information is displayed when the user views the version properties of the application s assembly (.dll) from within Microsoft Windows, as shown in Figure 9-4.
Figure 9-4. Assembly properties
To identify your application, open the AssemblyInfo file and enter the application s information in the assembly attributes. For example, the following attributes set the product name and version information shown in Figure 9-4:
Visual Basic .NET
Imports System.Reflection Imports System.Runtime.InteropServices ' General Information about an assembly is controlled through the following ' set of attributes. Change these attribute values to modify the information ' associated with an assembly. ' Review the values of the assembly attributes. <Assembly: AssemblyTitle("Web Flyer Storefront")> <Assembly: AssemblyDescription("Low-price reservation finder.")> <Assembly: AssemblyCompany("Contoso Ltd. ")> <Assembly: AssemblyProduct("Web Flyer")> <Assembly: AssemblyCopyright("2002")> <Assembly: AssemblyTrademark("Web Flyer is a trademark of Contoso Ltd.")> <Assembly: CLSCompliant(True)> ' The following GUID is for the ID of the typelib if ' this project is exposed to COM <Assembly: Guid("CE84F243-D7D9-4B7E-B43B-520A0D6B9B30")> ' Version information for an assembly consists of the following four values: ' ' Major Version ' Minor Version ' Build Number ' Revision ' ' You can specify all the values or you can default ' the Build and Revision Numbers by using ' the '*' as shown below: <Assembly: AssemblyVersion("2.1.*")>
Visual C#
using System.Reflection; using System.Runtime.CompilerServices; // // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. // [assembly: AssemblyTitle("Web Flyer Storefront")] [assembly: AssemblyDescription("Low-price reservation finder.")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Contoso Ltd.")] [assembly: AssemblyProduct("Web Flyer")] [assembly: AssemblyCopyright("2002")] [assembly: AssemblyTrademark("Web Flyer is a trademark of Contoso Ltd.")] [assembly: AssemblyCulture("")] // // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Revision and Build // Numbers by using the '*' as shown below: [assembly: AssemblyVersion("2.1.*")]
Two pieces of information are automatically generated for you in the AssemblyInfo file:
The Guid attribute is generated by Visual Studio .NET to identify the assembly to COM components. You need to change this number only if you break compatibility with earlier versions of the assembly by removing or significantly changing public objects and members that are used by COM components.
The AssemblyVersion attribute automatically updates the build version number, indicated by the asterisk (*). You can add levels of versioning or remove the build number if you want.
Configuring the Application
Web applications use text configuration files (.config) to specify how they run. A Web application s configuration file (Web.config) resides in the Web application s root directory, but that file s function is really to override the settings inherited from the following locations:
This sets the base configuration for all .NET assemblies running on the server.
This sets the base configuration for all Web applications and overrides settings in Machine.config.
These settings are inherited from Web.config files along the application s path.
The Web.config file uses Extensible Markup Language (XML) elements to control different aspects of the Web application, as described in Table 9-3. Because these elements use XML syntax, they are case sensitive and must be typed exactly as shown.
Element | Use to | For more information, see |
compilation | Set the build type to debug or release | Setting Build Options, earlier in this lesson |
customErrors | Display custom error pages in response to HTTP response codes | Chapter 6, Catching and Correcting Errors |
authentication | Set the type of authentication to use to identify users | Chapter 8, Maintaining Security |
authorization | List user names or user roles that are authorized to run the application | Chapter 8, Maintaining Security |
Trace | Enable tracing to help diagnose errors or tune performance | Chapter 6, Catching and Correcting Errors |
sessionState | Determine how Microsoft ASP.NET stores Session state variables | Lesson 3 in this chapter, and Chapter 3, Working with Web Objects |
globalization | Set cultural information for localized applications | Chapter 15, Globalizing Web Applications |
When you make changes to an application s Web.config file, IIS automatically restarts the application and applies the changes. This has the side effect of resetting current Application or Session state variables, which can adversely affect users.
For security reasons, you can t access the Web.config file from a browser. If a user requests the Web.config file from your Web site, he or she will receive an access denied error message.