Microsoft.NET

……………………………………………….Expertise in .NET Technologies

ASP.NET Application Folders – App_Code

Posted by Ravi Varma Thumati on July 16, 2009

When you create ASP.NET applications, notice that ASP.NET 2.0 now uses a file-based approach. When working with ASP.NET 2.0, 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 now 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 is no longer necessary because ASP.NET 2.0 applications have a defined folder structure. By 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. Take a look at each of these defined folders to see how they work. The first is the \App_Code folder.

\App_Code Folder

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 this folder, Visual Studio 2005 automatically detects this and compiles it if it is a class (.vb or .cs), 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 instantaneously available to any of your ASP.NET pages that are in the same solution. Look at how to employ a simple class in your solution using the \App_Code folder.

The first step is to create an \App_Code folder. To do this, simply right-click the solution and choose Add Folder➪App_Code Folder. Right away you will notice that Visual Studio 2005 treats this folder differently than the other folders in your solution. The \App_Code folder is shown in a different color (gray) with a document pictured next to the folder icon. See Figure.

asp folders1

After the \App_Code folder is in place, right-click the folder and select Add New Item. The Add New Item dialog that appears doesn’t give you many options for the types of files that you can place within this folder. The available options include 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 or Calculator.cs. Below example shows how the Calculator class should appear.

The Calculator class

VB

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

C#

using System;

public class Calculator

{

public int Add(int a, int b)

{

return (a + b);

}

}

What’s next? Just save this file, and it is now available to use in any pages that are in your solution. To see this in action, create a simple .aspx page that has just a single Label server control. Below example shows you the code to place within the Page_Load event to make this new class available to the page.

An .aspx page that uses the Calculator class

VB

<%@ 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>

C#

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

protected void Page_Load(object sender, System.EventArgs e)

{

Calculator myCalc = new Calculator();

Label1.Text = myCalc.Add(12, 12).ToString();

}

</script>

When you run this .aspx page, notice 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 instantaneously receive IntelliSense capability on the methods that the class exposes (as illustrated in Figure).

asp folders2

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 in Listing below.

Adding a Subtract method to the Calculator class

VB

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

C#

using System;

public class Calculator

{

public int Add(int a, int b)

{

return (a + b);

}

public int Subtract(int a, int b)

{

return (a – b);

}

}

After you have added the Subtract method to the Calculator class, save the file and go back to your .aspx page. Notice that the class has been recompiled by the IDE, and the new method is now available to your page. You see this directly in IntelliSense. Figure 3-10 shows this in action.

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. This means that 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).

asp folders3

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 to the web.config file.

The first step is to 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 won’t 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 this moment, so add one through the Solution Explorer. After it is added, change the <compilation> node so that it is structured as shown in Listing below example.

Structuring the web.config file so that classes in the \App_Code folder can use different languages

<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. Also, 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 as we did; you can use whatever name tickles your fancy.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: