The App_Code folder is meant to store classes, .wsdl files,
and typed datasets. Any of these items stored in this folder then is automatically
available to all the pages in the solution. To create an APP_Code Folder, we
can follow some steps. Right click on solution -> Select Add Folder ->
App_Code Folder. After adding App_Code Folder, we can add files on it. Right
Click on App_Code Folder will give the option to add different types of files
which include class file, text file, dataset, report and class diagram. Let us
add a class file named test.cs.
Listing 1
public class CurrentDateTime()
public string showDateTime()
{
return System.DateTime.Now.ToString();
}
Now the class test.css is available to all the pages in the
solution. To test the same an aspx page is created named test.aspx.
Listing 2
protected void Page_Load(object sender, EventArgs e)
{
CurrentDateTime cDT=new CurrentDateTime();
string cTime = cDT.showDateTime();
Response.Write(cTime.ToString());
}
When we run test.aspx page, we will see that it uses the
test.css class. So we do not need to compile the class file before it is in use.
It should be noted that whatever we place in the App_Code Folder, would be
complied into a single assembly. A class file written in C# can be used by the
pages written in a different language (say in Visual Basic 2005). Even we can
have class files written in different languages, but to work with them we have
to configure in the following ways: We should add two different folders in
App_Code Folder to place C# class and VB class separately. After that we should
do required modification in web.config file.
Listing 3
<compilation>
<codeSubDirectories>
<add directoryName=”VBFolder”></add>
<add directoryName=”CSFolder”></add>
</compilation>
So it is important that the language specific subdirectory
is registered in the web.config file; otherwise, all files underneath App_Code
folder will be complied to a single assembly regardless of the folder they
belong to. If a directory mentioned in the <codeSubDirectories> section
does not exist, compilation error will arise.