Bin and App_Code Folder Introduction _ Practical Tips

Source: Internet
Author: User
Tags wsdl
If your WEB application includes code that you want to share across multiple pages, you can save your code in a folder in two special folders (the Bin folder and the App_Code folder) under the root of the Web application.

Bin folder

You can store compiled assemblies in the Bin folder, and other code anywhere in the WEB application, such as page code, automatically references that folder. A typical example is the code that you compile for your custom class. You can copy the compiled assembly to the WEB application's Bin folder so that all pages can use this class.

Assemblies in the Bin folder do not need to be registered. asp.net can recognize a. dll file as long as it exists in the Bin folder. If you change the. dll file and write a new version of it to the Bin folder, ASP.net detects the update and uses a new version of the. dll file for subsequent new page requests.

Security for Bin folder

Putting a compiled assembly into the Bin folder poses a security risk. If you are writing and compiling your own code, then you understand the functionality of the code. However, if you are not writing and compiling your own code, you must treat the compiled code in the Bin folder as you would any executable code. Be cautious about the compiled code before you complete the code test and make sure you understand the functionality of the code.

Note the following security aspects, which are related to whether the compiled code is placed in the Bin folder:

The assembly in the Bin folder is scoped to the current application. As a result, they cannot access resources outside the current Web application or invoke code outside the current Web application.

At run time, the access level of the assembly is determined by the trust level specified on the local computer.

If you use a designer such as Visual Studio, the code in the Bin folder runs in a different context than the runtime. For example, your code might run in full trust.

App_Code folder

You can store source code in the App_Code folder, and the code will be compiled automatically at run time. Any other code in the WEB application can access the resulting assembly. Therefore, the App_Code folder works in a similar way to the Bin folder, where you can store source code instead of compiled code. The App_Code folder and its special status 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 a traditional class file (that is, a file with an extension such as. vb,. cs, etc.). However, it can also contain files that are not explicitly displayed by a particular programming language. For example. wsdl (Web Service Discovery language) file and XML schema (. xsd) file. ASP.net can compile these files into assemblies.

Depending on your needs, the App_Code folder can contain any number of files and subfolders. You can organize your 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.

Infer the programming language of the App_Code folder

The App_Code folder is not explicitly marked as containing files written in any one programming language. Instead, asp.net infers which compiler should be invoked for the App_Code folder based on the files contained in the App_Code folder. If the App_Code folder contains a. vb file, ASP.net uses the Visual Basic compiler, and if the. cs file is included, 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 (such as a. wsdl file), then ASP.net will use the default compiler for the Web application, and the default compiler is in the <compilation> element of the Web application or machine configuration file Set

Use multiple programming languages in the App_Code folder

Because the source code in the App_Code folder is 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 both Visual Basic and C #.

However, you can configure the WEB application to handle subfolders of the App_Code folder as separate, programmable units. In this way, each folder can contain source code written in different programming languages. You can specify this configuration by creating a <codeSubDirectories> element in the <compilation> element of the Web.config file and then adding a reference to the subfolder. The following example illustrates how to configure a subfolder named Vbcode and Cscode to compile into a different assembly:

<compilation debug= "false" > <codeSubDirectories> <add directoryname= "Vbcode"/> <add Directoryname= "Cscode"/> </codeSubDirectories></compilation>

Note that references to Vbcode and Cscode subfolders do not include any information about the programming language contained in the subfolder. As with the App_Code folder itself, asp.net infers the compiler to use based on the files in the subfolder.

Security for the App_Code folder

The security issues with code in the App_Code folder are essentially the same as the security issues in the code in the Bin folder-code is compiled into an assembly and executed at run time. Better than the Bin folder, you can read the source code for files in the App_Code folder. However, if you do not fully understand the code, there is still a security risk. Therefore, the attitude towards the source code in the App_Code folder must be as cautious as 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.