Is there some initialization area in ASP.NET that only runs once?
I've got a piece of code that I want to run one time when m开发者_JAVA百科y website is first loaded. It is basically a static initialization of another module.
Is there some event I can hook into that runs when the site is first served to a client? It doesn't even need to be per session, just on first load to any client.
I'm using vs2010, .net 4.0, asp.net 4.0
You can use the Application_Start event.
See MSDN ASP.NET Application Life Cycle Overview
Application_Start is Called when the first resource (such as a page) in an ASP.NET application is requested. The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values.
You can also use PreApplicationStartMethodAttribute, available as of ASP.NET 4.0. See this article for an example.
精彩评论