ASP.NET - 动态注册在code一个HttpHandler(而不是在web.config中) [英] ASP.NET - Dynamically register an HttpHandler in code (not in web.config)
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/1888016/any-way-to-add-httphandler-programatically-in-net\">Any方法添加的HttpHandler编程在.NET?
有没有一种方法,我可以动态地在C#code寄存器的的IHttpHandler
,而不必手动将其添加到系统。在web.config网/ HttpHandlers的
部分。
Is there a way I can dynamically register an IHttpHandler
in C# code, instead of having to manually add it to the system.web/httpHandlers
section in the web.config.
这听起来很疯狂,但我有很好的理由这样做。我建立一个WidgetLibrary,一个网站的所有者可以通过删除一个.dll文件到他们的bin目录中只使用了,想用最少的配置到web.config支持这一点。
This may sound crazy, but I have good reason for doing this. I'm building a WidgetLibrary that a website owner can use just by dropping a .dll file into their bin directory, and want to support this with minimal configuration to the web.config.
推荐答案
我不相信这是可以修改注册HttpHandlers的,一旦AppDomain中运行时,由于可用的处理程序是从网络直接读取的.config
文件,然后缓存在一个私有数据结构。
I don't believe it's possible to modify the registered HttpHandlers once the AppDomain is running because the available handlers is read directly from the web.config
file then cached in a private data structure.
如果您知道前期你想它扩展,允许,你可以做的就是这些扩展名映射到一个 HttpHandlerFactory
,然后返回您所选择的处理程序(通过使用动态集加载和反射)。例如:
If you knew upfront which extensions you wanted to allow, you could do is map these extensions to a single HttpHandlerFactory
and then return a handler of your choice (by using dynamic assembly loading and reflection). For example:
<add path="*.ch1,*.ch2,*.ch3" verb="*"
type="MyHandlers.MyHandlerFactory, MyHandlers" />
修改的web.config
在运行时会导致AppDomain重新启动。
Modifying the web.config
at runtime would cause the AppDomain to restart.
这篇关于ASP.NET - 动态注册在code一个HttpHandler(而不是在web.config中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!