My understanding of app_code

Source: Internet
Author: User
My personal understanding of app_code:
Create a general class for the whole site. Compile the. CS file under the app_code folder when you open the website.
Classes in The. CS file under the app_code folder can be called in each file ..

My personal understanding of namespace:
For example, there is namespace Testa in app_code/a. CS.
The default. aspx. CS file in the root directory of the website also contains namespace Testa. In this file, you can directly call the class name in app_code/a. CS. If the default. aspx. CS file in the root directory of the website is namespace testb, you must use the following reference to use the class name of namespace Testa in app_code/a. CS: Using Testa;

The following is an excerpt from the app_code directory.Article:

ASP. NET 2.0 introduces the app_code Directory, which can contain some independent files that are included in the application Program Shared between multiple pages in Code . Unlike ASP. NET 1.x (1.x needs to pre-compile these files to the bin directory), all the code files in the app_code directory will be dynamically compiled at runtime and then provided to the application. You can place files in multiple languages under the app_code directory, provided that these files are divided into subdirectories (these subdirectories are registered in specific languages in Web. config ). By default, the app_code directory can only contain files in the same language. However, the app_code directory can be divided into several sub-directories (each sub-directory contains files in the same language) so that the app_code directory contains multiple languages. Therefore, you must register each subdirectory in the web. config file of the application.
<System. Web>
<Add directoryname = "subdirectory"/>
</System. Web>

The above is an official description of app_code. The description mentions "dynamic compiling during runtime". To find out when the files under the app_code directory are compiled, a program is written for testing.
Testclass. CS
Public class testclass
Public static int m_count = 0;
Public testclass ()
M_count ++;
Public String getmessage (string Str)
Return "Hello:" + STR;
Code on the ASPX page
Testclass T = new testclass (); %>
<% = T. getmessage ("softdiyer") %> <br>
M_count = <% = testclass. m_count %>
I put the code on two aspx (test1.aspx and test2.aspx) pages. I first asked test1.aspx, m_count = 1; then I accessed test2.aspx m_count = 2; and modified testclass. access test2.aspx and m_count = 1 again in CS. the. NET Framework monitors the app_code Directory, which is triggered when files in the Directory change. net.

app_code, ASP include ,. net import comparison:
app_code is different from include in ASP: app_code adopts the compilation mechanism while include in ASP adopts the interpretation mechanism, on the surface, the purpose and method of implementation are a bit similar, but the processing method is completely different. Use Code Directories to make programs more efficient and easier to maintain.
app_code differs from import: the difference between the call code is that the import statement must be used in the file when the import is used, and the code directory method is not required. Generally, the Import Statement uses compiled classes and has been compiled before running. The Code directory is compiled at runtime, which increases the flexibility of the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.