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:
- 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.