gpt4 book ai didi

asp.net - 在运行时以编程方式注册 HttpModule

转载 作者:行者123 更新时间:2023-12-02 03:05:01 26 4
gpt4 key购买 nike

我正在编写一个应用程序,第三方供应商可以在其中编写插件 DLL 并将它们放入 Web 应用程序的 bin 目录中。我希望这些插件能够在必要时注册自己的 HttpModule。

无论如何,我是否可以在运行时在管道中添加或删除 HttpModule,而无需在 Web.Config 中添加相应的条目,或者在添加/删除模块时是否必须以编程方式编辑 Web.Config?我知道无论哪种方式都会导致 AppDomain 重新启动,但我宁愿能够在代码中完成此操作,而不是必须修改 web.config 才能达到相同的效果。

最佳答案

It has to be done at just the right time in the HttpApplication life cycle which is when the HttpApplication object initializes (multiple times, once for each instance of HttpApplication). The only method where this works correct is HttpApplication Init().

To hook up a module via code you can run code like the following instead of the HttpModule definition in web.config:

  public class Global : System.Web.HttpApplication
{
// some modules use explicit interface implementation
// by declaring this static member as the IHttpModule interface
// we work around that
public static IHttpModule Module = new xrnsToashxMappingModule();
public override void Init()
{
base.Init();
Module.Init(this);
}
}

All you do is override the HttpApplication's Init() method and then access the static instance's Init method. Init() of the module hooks up the event and off you go.

通过 Rick Strahl's blog

关于asp.net - 在运行时以编程方式注册 HttpModule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/239802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com