When you create ASP.NET applications, ASP.NET 2.0 uses a file-based approach. You can add as many files and folders as you want within your application without recompiling each and every time a new file is added to the overall solution. ASP.NET 2.0 includes the capability to automatically precompile your ASP.NET applications dynamically.
ASP.NET 1.0/1.1 compiled everything in your solution into a DLL. This isn’t necessary with ASP.NET 2.0 because applications have a defined folder structure. Using the ASP.NET 2.0 defined folders, you can have your code automatically compiled for you, your application themes accessible throughout your application, and your globalization resources available whenever you need them. The following sections look at each of these defined folders.
The \App_Code folder is meant to store your classes, .wsdl files, and typed datasets. Any of these items stored in this folder are then automatically available to all the pages within your solution. The nice thing about the \App_Code folder is that when you place something inside it, Visual Studio 2005 automatically detects this and compiles it if it is a class (such as a .vb file), automatically creates your XML Web service proxy class (from the .wsdl file), or automatically creates a typed dataset for you from your .xsd files. After the files are automatically compiled, these items are then immediately available to any of your ASP.NET pages in the same solution. Let’s look at how to employ a simple class in your solution using the \App_Code folder.
Create an \App_Code folder by right-clicking the solution and choosing Add ASP.NET Folder ⇒ App_Code. Note that Visual Studio 2005 treats this folder differently than the other folders in your solution. The \App_Code folder appears in a different color (gray), with a document pictured next to the folder icon (see Figure 19-9).
Figure 19-9
After the \App_Code folder is in place, right-click the folder and select Add New Item. The Add New Item dialog that appears offers only a few options for the types of files that you can place within this folder, including a Class file, a Text file, a DataSet, a Report, and a Class Diagram if you are using Visual Studio 2005. Visual Web Developer 2005 Express Edition offers only the Class file, Text file, and DataSet file. For the first example, select the file of type Class and name the class Calculator.vb. The following code shows how the Calculator class should appear:
Imports Microsoft.VisualBasic Public Class Calculator Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer Return (a + b) End Function End Class
Simply save this file, and it is now available to use in any pages in your solution. To see this in action, create a simple .aspx page that has just a single Label server control. The following example shows the code to place within the Page_Load event to make this new class available:
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim myCalc As New Calculator Label1.Text = myCalc.Add(12, 12) End Sub </script>
When you run this .aspx page, note that it utilizes the Calculator class without any problem, with no need to compile the class before use. In fact, right after saving the Calculator class in your solution or moving the class to the \App_Code folder, you also immediately receive IntelliSense capability on the methods that the class exposes (as illustrated in Figure 19-10).
Figure 19-10
To see how Visual Studio 2005 works with the \App_Code folder, open the Calculator class again in the IDE and add a Subtract() method. Your class should now appear as shown here:
Imports Microsoft.VisualBasic Public Class Calculator Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer Return (a + b) End Function Public Function Subtract(ByVal a As Integer, ByVal b As Integer) As Integer Return (a - b) End Function End Class
After you add the Subtract() method to the Calculator class, save the file and go back to your .aspx page. Note that the class has been recompiled by the IDE, and the new method is now available to your page (see Figure 19-11). You see this directly in IntelliSense.
Figure 19-11
Everything placed in the \App_Code folder is compiled into a single assembly. The class files placed within the \App_Code folder are not required to use a specific language. For example, even if all the pages of the solution are written in Visual Basic 2005, the Calculator class in the \App_Code folder of the solution can be built in C# (Calculator.cs).
Because all the classes contained in this folder are built into a single assembly, you cannot have classes of different languages sitting in the root \App_Code folder, as in the following example:
\App_Code Calculator.cs AdvancedMath.vb
Having two classes made up of different languages in the \App_Code folder (as shown here) causes an error to be thrown. It is impossible for the assigned compiler to work with two different languages. Therefore, in order to be able to work with multiple languages in your \App_Code folder, you must make some changes to the folder structure and the web.config file.
First, add two new subfolders to the \App_Code folder: a \VB folder and a \CS folder. This gives you the following folder structure:
\App_Code \VB Add.vb \CS Subtract.cs
This still will not correctly compile these class files into separate assemblies, at least not until you make some additions to the web.config file. Most likely, you don’t have a web.config file in your solution at the moment, so add one through the Solution Explorer. After it is added, change the <compilation> node so that it is structured as shown here:
<compilation> <codeSubDirectories> <add directoryName="VB"></add> <add directoryName="CS"></add> </codeSubDirectories> </compilation>
Now that this is in place in your web.config file, you can work with each of the classes in your ASP.NET pages. In addition, any C# class placed in the CS folder is now automatically compiled just like any of the classes placed in the VB folder. Because you can add these directories in the web.config file, you are not required to name them VB and CS; you can use whatever names you want.
The \App_Data folder holds the datastores used by the application. It is a good spot to centrally store all the datastores your application might use. The \App_Data folder can contain Microsoft SQL Express files (.mdf files), Microsoft Access files (.mdb files), XML files, and more.
The user account utilized by your application has read and write access to any of the files contained within the \App_Data folder. By default, this is the ASPNET account. Another reason to store all your data files in this folder is that much of the ASP.NET system - from the membership and role management systems to the GUI tools such as the ASP.NET MMC snap-in and ASP.NET Web Site Administration Tool - is built to work with the \App_Data folder.
Themes are a new way of providing a common look-and-feel to your site across every page. You implement a theme by using a .skin file, CSS files, and images used by the server controls of your site. All these elements can make a theme, which is then stored in the \App_Themes folder of your solution. By storing these elements within the \App_Themes folder, you ensure that all the pages within the solution can take advantage of the theme and easily apply its elements to the controls and markup of the page.
Resource files are string tables that can serve as data dictionaries for your applications when they require changes to content based on things such as changes in culture. You can add Assembly Resource files (.resx) to the \App_GlobalResources folder, and they are dynamically compiled and made part of the solution for use by all your .aspx pages in the application. When using ASP.NET 1.0/1.1, you had to use the resgen.exe tool and compile your resource files to a .dll or .exe for use within your solution. Now it is considerably easier to deal with resource files in ASP.NET 2.0. Simply place your applicationwide resources in this folder to make them instantly accessible.
Even if you are not interested in constructing applicationwide resources using the \App_GlobalResources folder, you may want resources that can be used for a single .aspx page. You can do this very simply by using the \App_LocalResources folder.
Add page-specific resource files to the \App_LocalResources folder by constructing the name of the .resx file in the following manner:
Default.aspx.resx
Default.aspx.fi.resx
Default.aspx.ja.resx
Default.aspx.en-gb.resx
Now, the resource declarations used on the Default.aspx page are retrieved from the appropriate file in the \App_LocalResources folder. By default, the Default.aspx.resx resource file is used if another match is not found. If the client is using a culture specification of fi-FI (Finnish), however, the Default.aspx.fi.resx file is used instead.
The \App_WebReferences folder is a new name for the Web References folder used in previous versions of ASP.NET. Using the \App_WebReferences folder, you have automatic access to the remote Web Services referenced from your application.
The \App_Browsers folder holds .browser files, which are XML files used to identify the browsers making requests to the application and understand the capabilities of these browsers. You can find a list of globally accessible .browser files at C:\Windows\Microsoft.NET\Framework\v2.0.50727\ CONFIG\Browsers. In addition, if you want to change any part of these default browser definition files, just copy the appropriate .browser file from the Browsers folder to your application’s \App_Browsers folder and change the definition.