App_Code folder and Bin folder

Source: Internet
Author: User

Learn at: http://msdn.microsoft.com/zh-cn/library/t990ks23 (v = vs.100). aspx

If a website project contains code to be shared by multiple pages, you can save the code to a folder in two special folders under the root directory of the Web application: compiled code (. dll files) in the Bin folder, and the source code in the App_Code folder. ASP. NET processes these files in special ways when you create these folders and store specific types of files in them.

1. Bin folder

The compiled assembly (. dll file) can be stored in the Bin folder, and other code (such as page Code) in any part of the Web application will automatically reference this folder. A typical example is the code compiled for the custom class. You can copy the compiled assembly to the Bin folder of the Web application so that this class can be used on all pages.

The Assembly in the Bin Folder does not need to be registered. ASP. NET can recognize the. dll file as long as it exists in the Bin folder. If you have changed. dll file, and write the new version to the Bin folder, ASP. NET will detect updates and use the new version for subsequent new page requests. dll file.

Note: The Bin folder is used to host code assembly instead of local code (unmanaged code) assembly.

1.1 Bin Folder Security

Putting the compiled assembly into the Bin folder brings security risks. If the code is compiled and compiled by yourself, you can understand the functions of the Code. However, you must treat compiled code in the Bin folder like any executable code. Be cautious with compiled code before you complete code testing and are sure you have understood the code function.

Pay attention to the following security knowledge about whether to put compiled code into the Bin Folder:

  • The range of the Assembly in the Bin folder is the current application. Therefore, they cannot access resources outside the current Web application or call code outside the current Web application.

  • During runtime, the access level of the Assembly is determined by the trust level specified on the local computer.

  • When running a project in Visual Studio, the context of the code running in the Bin folder is different from that in the runtime. The Visual Studio development server runs with the logon ID. If you log on as an administrator, the code runs as administrator. However, when a website is deployed, the Code usually does not run as administrator. Therefore, the IIS server should be used for testing before the project is deployed to ensure that no security error occurs in production.

2. App_Code folder

In a website project, you can store source code in the App_Code folder. The code is automatically compiled at runtime. Any other code in the Web application can access the generated assembly. Therefore, the App_Code folder works in a similar way as the Bin folder. The difference is that you can store source code instead of compiled code. App_Code folder and its contents in ASP. NET Web applications allow you to create custom classes and other source-only files and use them in Web applications without having to compile them separately.

The App_Code folder can contain source code files written in the form of traditional class files (files with. vb,. cs, and other extensions. However, it can also contain files that are not explicitly displayed in a specific programming language. For example, the. wsdl (Web Service Description Language) file and the XML schema (. xsd) file. ASP. NET can compile these files into an assembly.

The App_Code folder can contain any number of files and subfolders as needed. You can organize the source code in any way you think is convenient. ASP. NET will still compile all the code into a single assembly, and other code anywhere in the Web application can access the assembly.

Note:

User Controls (ascx files) cannot exist in the App_Code folder ). This includes a single-file user control and a user control that uses the code to hide the model. Placing a user control in the App_Code directory does not allow compilation in the order required by the user control code. (In any case, you do not need to place user controls in the App_Code folder, because pages anywhere in the application can already use these controls .)

2.1 deduce the programming language of the App_Code folder

The App_Code folder is not explicitly marked as containing Files written in any programming language. On the contrary, ASP. NET deduce which compiler should be called for the App_Code folder Based on the files contained in the App_Code folder. If the App_Code folder contains the. vb file, ASP. NET uses the Visual Basic compiler. if it contains the. cs file, ASP. NET uses the C # compiler, and so on.

If the App_Code folder contains only files that do not explicitly indicate the programming language (for example. (wsdl file), ASP. NET will use the default compiler of the Web application, the default compiler in the Application Web. config file or computer-level Web. the compilation element in the config file. The compiler is the specified generation provider, which is specified for each file extension in the extension element.

2.2 use multiple programming languages in the App_Code folder

Because the source code in the App_Code folder must be compiled into a single assembly, all files in the App_Code folder must be written in the same programming language. For example, the App_Code folder cannot contain source code written in Visual Basic and C # at the same time.

However, you can configure a Web application to process the subfolders in the App_Code folder as independent compilation units. In this way, each folder can contain the source code written in different programming languages. Create a compilation element in the codeSubDirectories element of the Web. config file and add a reference to the sub-folder to specify the configuration. The following example illustrates how to configure the subfolders VBCode and CSCode to compile them into different assemblies:

<compilation debug="false">    <codeSubDirectories>        <add directoryName="VBCode" />        <add directoryName="CSCode" />    </codeSubDirectories></compilation>

References to VBCode and CSCode subfolders do not need to include any information about the programming languages contained in subfolders. Just like the App_Code folder itself, ASP. NET will deduce the compiler to be used based on the files in the subfolders.

2.3 security of the App_Code folder

The security issues of the Code in the App_Code folder are basically the same as those of the Code in the Bin folder-the code will be compiled into an assembly at runtime. Better than the Bin folder, you can read the source code of the file in the App_Code folder. However, if you do not fully understand the code, there are still security risks. Therefore, the attitude towards the source code in the App_Code folder must be as cautious as with the compiled code generated based on the same source code.

 

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: info-contact@alibabacloud.com 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.