Sun*_*oot 13 c# asp.net web-config httphandler ihttphandler
有没有办法可以动态注册IHttpHandlerC#代码,而不必手动将其添加到system.web/httpHandlersweb.config中的部分.
这可能听起来很疯狂,但我有充分的理由这样做.我正在构建一个WidgetLibrary,网站所有者只需将.dll文件放入他们的bin目录就可以使用,并希望以最少的配置支持web.config.
Kei*_*h K 18
您无法修改处理程序,但可以按照以下步骤以编程方式向您的处理程序添加路径:
实现IRouteHandler
public class myHandler : IHttpHandler, IRouteHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
// your processing here
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
注册路线:
//from global.asax.cs
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new Route
(
"myHander.axd",
new myHandler()
));
}
Run Code Online (Sandbox Code Playgroud)
你有它.通过代码注册的手铐.:)
我不相信一旦AppDomain运行就可以修改已注册的HttpHandler,因为可用的处理程序直接从web.config文件中读取,然后缓存在私有数据结构中.
如果您事先知道要允许哪些扩展,那么您可以将这些扩展映射到单个扩展HttpHandlerFactory,然后返回您选择的处理程序(通过使用动态程序集加载和反射).例如:
<add path="*.ch1,*.ch2,*.ch3" verb="*"
type="MyHandlers.MyHandlerFactory, MyHandlers" />
Run Code Online (Sandbox Code Playgroud)
修改web.config运行时将导致AppDomain重新启动.