The app_code folder is located in the root directory of the Web application. It stores all class files that should be dynamically compiled as part of the application. These class files are automatically linked to the application without adding any explicit commands or declarations on the page to create dependencies. The class files placed in the app_code folder can contain any identifiable ASP. NET component-custom controls, helper classes, build providers, business classes, custom providers, and HTTP processing programs.
Note that during development, changes to the app_code folder will cause the entire application to be re-compiled. This may be undesirable and time-consuming for large projects. To this end, we encourage you to modularize the code into different class libraries and organize the code according to the logically related class sets. Most of the helper classes dedicated to applications should be placed in the app_code folder.
All class files stored in the app_code folder should use the same language. If a class file is written in two or more languages, you must create subdirectories in a specific language to contain classes written in each language. Once these class files are organized by language, you must add a setting for each subdirectory in the web. config file:
<Compilation>
<Codesubdirectories>
<Add directoryname = "vbfolder"/>
</Codesubdirectories>
</Compilation>
It is important that subdirectories in specific languages should be registered in the web. config file. Otherwise, all files in the app_code folder will be compiled into a separate assembly no matter which folder they belong. The preceding configuration script describes the situation where all C # files are stored in the root directory of the app_code folder, and several Visual Basic. Net files are moved to the vbfolder directory. If the directory mentioned in the <codesubdirectories> section does not exist, a compilation error is reported.
Files in the app_code root folder are compiled into the app_code_xxx.dll assembly, where XXX is a random character sequence. The file in a given subdirectory will be compiled into a dynamically created Assembly named app_subcode_xxx_yyy.dll. xxx indicates the subdirectory name, And YYY indicates a random character sequence. The <codesubdirectories> section is valid only when the web. config file in the application root directory is set.
Place an assemblyinfo. CS file in the app_code directory or any other sub-directories to create a strongly-named assembly. Obviously, if the folder contains the Visual Basic. Net file, the assemblyinfo. VB file will be used. The assembly configuration file can reference A. SNK file to save the key with a strong name.
Note: To set a strong name for an assembly, you must first obtain a public/private key pair. You can obtain such a key pair by using the strong name (strong name1_tool (sn.exe. The strong name tool is one of the SDK binary files that we can find in the. NET Framework installation path. A key pair file usually has a. SNK extension. You can save the file to an application folder and reference it in the assemblyinfo. CS file, as shown below:
[Assembly: assemblykeyfileattribute (@ "yourkeypair. SNK")]
Note that Visual Basic. NET is used to find the key file in the directory containing Visual Studio solution, while the C # compiler searches for the key file in the directory containing the binary file. Therefore, we can use this attribute to adjust the path we are using or place the key file in a suitable folder.
In any subsequent re-generation, the Assembly name will change. At the same time, when the old appdomain request ends, the old assembly will be deleted.
The app_code Folder does not only contain class files. In particular, it can contain and can automatically process XSD files representing the data architecture. When you add an XSD file to this folder, the compiler parses it into a dataset class with a type and adds it to the application scope. In ASP. NET 1. X, this job is directed by Visual Studio .netand completed by a command line program (xsd.exe.
Note: When registering a component (for example, a custom server control or a custom HTTP handler) using the web. config file, you must specify the Assembly name that contains the code. If the component is defined in the app_code folder, what name should be used to indicate the assembly? In this case, you only need to ignore the assembly information and specify the complete class name. If no collation set is specified, the ASP. NET runtime attempts to load the class from any mounted assembly, including the Assembly dynamically created for the app_code folder.
Example program:
1. Use C # To create a new Asp.net application, which means that the entire web application will mainly adopt the C # language.
2. modify the configuration file and register directories that can use multiple languages.
<Compilation DEBUG = "true">
<Codesubdirectories>
<Add directoryname = "vb_code"/>
</Codesubdirectories>
</Compilation>
3. Add the VB class to the specified directory.
Imports Microsoft. VisualBasic
Public class vbobject
Public Function formatstring (byval inputstr as string)
Return "this string comes from the VB. NET object" & inputstr
End Function
End Class
4. Add C # class
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
/// <Summary>
/// Summary of csharpobject
/// </Summary>
Public class csharpobject
{
Public csharpobject ()
{
//
// Todo: add the constructor logic here
//
}
Public String formatstring (string inputstr)
{
Return "this string comes from the C # object" + inputstr;
}
}
4. Create a page to test the use of the two classes defined above
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Vbobject VBO = new vbobject ();
Response. Write (VBO. formatstring ("Hello !!! ") +" <Br> ");
Csharpobject csharpo = new csharpobject ();
Response. Write (csharpo. formatstring ("Hello !!! ") +" <Br> ");
}
}