Several initialization methods when ASP. NET applications are started

Source: Internet
Author: User

Before ASP. NET 4.0, there are two methods: Start through the application_start event in global. asax, or define the appinitialize static method in any class in the app_code folder. The method prototype is defined as follows:

 
 
  1. public static void AppInitialize()

The appinitialize method is a special method in ASP. NET. It will be called first after the website application is started. This method can only appear once and can only appear in a class defined in app_code. If it appears in two classes, ASP. NET will report a compilation error. This method cannot even be defined in a class defined in a set of programs.

In ASP. net 4.0 has an interesting feature: Pre Application Start method. The new pre Application Start method mechanism slightly changes this process (originally in ASP. net, when the application starts, the first request is global. the application_start function in asax). When an assembly is labeled with preapplicationstar method attribute, Asp. net will call the specified function at the beginning of the application. This action occurs in global. before application_start in asax, assemblys in the bin directory is automatically scanned. For more information, see ASP. NET 4.0-pre Application Start method.

To sum up, there are three methods to initialize ASP. NET applications:

1. Complete initialization in the application_start function in global. asax, which is also the most used and not described in detail.

2. appinitialize static method in any class in the app_code folder. the app_code directory is a very special directory, and you do not know many rules, Asp. net will perform some special processing on the files in this directory. For example, the execution rules of the appinitialize static method:

  • The static appinitialize method must be in the app_code directory.
  • The appinitialize static method cannot be defined twice in the app_code directory.
  • The appinitialize method is specially processed during compilation, and cannot be defined in the external application Assembly DLL.

3. In preapplicationstartmethod, We initialize the website before initialization. In this way, we can use this feature in a class library project to mark the method that needs to be initialized in advance on the website. For more information, see ASP. NET 4.0-pre Application Start method.

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.