4

我正在创建一个需要在 ASP.NET 请求生命周期的各个阶段运行一些代码的库。

通过重写 HttpApplication.Init() 并在其中注册各种处理程序来编写 Web 应用程序时,很容易做到这一点。

但是,我想在 Application_Start(); 期间从我的库中执行此操作;我不想让消费者不得不重写 Init() 并从那里调用我的事件设置代码。

有什么办法可以做到这一点?

这是我的理想设置:

// in the consumer's code
protected void Application_Start()
{
    // should ideally be able to register lifecycle event handlers from the line below
    MyLibrary.Configure(...); 
}
4

1 回答 1

0

你应该能够做到

全球.asax

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        MyLib.Configure(this);
    }
}

这里指的是应用程序,您可以在其上订阅大量事件,包括请求生命周期事件

于 2014-01-22T17:45:51.063 回答