The role and usage scenario of the App_Code folder in an ASP. NET Web site

Source: Internet
Author: User

Original Address:The role of the App_Code folder in Web site projects and ASP. NET Web Application Bing's Guest

I'm going to build a Web site now, not hard, start. Such as:


This method builds a Web site project, and the used developers know that if you need to create a new class, such as DataBase.cs. This class serves only Web pages and does not fit into the site's own class library, so you want it to exist independently of the class library, but to be called by a Web page in a Web application. This is best done by forming a DLL file and placing it in the Bin folder for a Web page call. However, if you place the DataBase.cs source file under the Web site root directory, the class cannot be called on the Web page. However, if you add an App_Code folder by using the "right-click Project-〉 Add-〉 Add ASP." method, such as:

Put DataBase.cs in this folder, you can call directly, why?? The source code can be stored directly in the App_Code folder, which is automatically compiled by the Web site at run time. Any other code in the WEB application can access the assembly it produces. Therefore, the App_Code folder works much like the Bin folder, where you can store source code rather than 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 a Web application without having to compile them separately.     Well, I can directly use the source code files, and do not need to call the DLL file, looks very good, save a lot of things. Summarize the differences between the App_Code folder and the Bin folder:
The 1:app_code folder stores the source files of the class, and the Bin folder stores the class Library's. dll files, which are already compiled.
2: When using the App_Code folder class, you do not need to add any displayed directives or declarations on the page to create the dependencies, and to use the classes in the Bin folder, you must first refer to the corresponding namespaces on the page before you can use them. Next, to do something, Web site seems a bit simpler, to a multi-layered architecture of the ASP. NET Web application how to do it. Also remember to first set up the App_Code folder, the province can not be called, such as:
The problem comes out, how actually did not app_code this folder, then the new set up a normal folder, from such a name, it looks exactly the same, and then set up DataBase.cs class, you should be able to use it. Add file code, such as: Error message: "The type or namespace name ' DataBase ' could not being found (is you missing a using directive or an Asse mbly reference ";
Crash, why not, calm down, it's obvious. In the App_Code folder, the code that will be automatically compiled when the WEB site project runs is not compiled in this project.     So now the DataBase.cs is just a file, he will not be compiled during the execution of the project, and will not be used by other program files, so this error is normal, then OK, manually modify it. Select your class file,-〉 the default "Content" to "Compile" in the Properties-〉build action, such as: When should I use the App_Code folder to create the required classes? Wait for me to find

The role and usage scenario of the App_Code folder in an ASP. NET Web site

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.