当AppInitialize方法在ASP.NET被调用? [英] When AppInitialize method get invoked in ASP.NET?

查看:277
本文介绍了当AppInitialize方法在ASP.NET被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在自定义的VirtualPathProvider的实践中,我发现它自定义的VirtualPathProvider可以在Global.asax中或AppInitialize方法根据MSDN <一注册href=\"http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx\">http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx.但是,MSDN没有明确描述方法AppInitialize。

During practice of customizing VirtualPathProvider, I found that it the custom VirtualPathProvider can be registered in Global.asax or in AppInitialize method according to MSDN http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx. However, MSDN doesn't clearly describe the method AppInitialize.

在是否APP_ code文件夹中的静态方法AppInitialize将自动由ASP.NET运行时调用的启动?

Does any static AppInitialize method in App_code folder will be automatically invoked by ASP.NET runtime at start up?

推荐答案

虽然有precious有关AppInitialize()方法的文档很少,你是正确的在你的假设,即在APP_ code文件夹中的任何类包含方法签名是这样的:

While there is precious little documentation about the AppInitialize() method, you are correct in your assumption that any class in your App_Code folder that contains a method signature like this:

public static void AppInitialize()

当Asp.Net应用程序启动

将被调用。请记住,APP_ code是一个特殊的文件夹Asp.Net,一切里边有被视为有点不同。祝你好运在APP_ code文件夹中的所有的小怪癖(如前述)查找文档。

will be invoked when the Asp.Net application starts up. Remember that App_Code is a special folder to Asp.Net and everything inside there is treated a little differently. Good luck finding documentation on all the little quirks (like the aforementioned) of the App_Code folder.

不过要记住的另一件事是,只有一个类可以包含一个签名为AppInitialize()方法,否则你会在运行时获取一个类似的编译器错误:

Another thing to remember however is that only one class can contain a signature for the AppInitialize() method or else you will get a compiler error at runtime similar to this:

该AppInitialize方法被定义
  无论是在APP_ code.SomeClassOne和
  APP_ code.SomeClassTwo。

The AppInitialize method is defined both in 'App_Code.SomeClassOne' and in 'App_Code.SomeClassTwo'.

因此​​,虽然这是完全正确的:

So while this is perfectly valid:

public class SomeClassOne
{
    public static void AppInitialize()
    {
        HostingEnvironment.Cache["InitializationTimeOne"] = DateTime.Now;
    } 
}

这将产生编译器错误我上面提到的:

This will generate the compiler error I mentioned above:

public class SomeClassOne
{
    public static void AppInitialize()
    {
        HostingEnvironment.Cache["InitializationTimeOne"] = DateTime.Now;
    } 
}

public class SomeClassTwo
{
    public static void AppInitialize()
    {
        HostingEnvironment.Cache["InitializationTimeTwo"] = DateTime.Now;
    } 
}

我希望这清除了一点东西给你:)

I hope this clears things up a bit for you :)

这篇关于当AppInitialize方法在ASP.NET被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆