Use preapplicationstartmethod to register the custom virtualpathprovider {}

Source: Internet
Author: User

In ASP.. Net website, some work must be completed in the website initialization stage, such as the custom virtual path provider. Generally, we need. register in asax, or use the appinitialize static method of any class in the app_code directory. These two methods are discussed in the question of virtualpathprovider registration in coderzh.

 

In ASP. net 4.0 provides a new feature preapplicationstartmethod that allows us to complete website initialization before the above initialization. In this way, we can use this feature in a class library project to mark methods that need to be initialized in advance on the website. However, the sequence of the application startup methods defined by the Assembly cannot be guaranteed when multiple assemblies are called. Therefore, each registration start method should write the code for separate operation, and should not depend on the side effects of other registration start methods.

For more information, see the msdn preapplicationstartmethodattribute class.

 

The following code demonstrates how to register a custom virtualpathprovider. Only this project can be referenced in a website project without additional configuration.

 

Code 1 using system;
2 using system. Collections. Generic;
3 using system. LINQ;
4 using system. text;
5
6 using system. Web;
7
8 [Assembly: preapplicationstartmethod (typeof (mynamespace. class1), "init")]
9
10 namespace mynamespace
11 {
12
13 public class demovirtualpathprovider: system. Web. Hosting. virtualpathprovider
14 {
15}
16
17 public class class1
18 {
19 public static void Init ()
20 {
21 demovirtualpathprovider provider = new demovirtualpathprovider ();
22 system. Web. Hosting. hostingenvironment. registervirtualpathprovider (provider );
23}
24}
25}
26

 

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.