以任何方式在.NET中以编程方式添加HttpHandler吗? [英] Any way to add HttpHandler programmatically in .NET?

查看:82
本文介绍了以任何方式在.NET中以编程方式添加HttpHandler吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在对此进行研究,但是没有找到答案-有什么方法可以以编程方式将HttpHandler添加到ASP.NET网站而无需添加到web.config?

I've been researching this a bit but haven't come across an answer -- is there any way to programatically add an HttpHandler to an ASP.NET website without adding to the web.config?

推荐答案

通过添加HttpHandler,我假设您是指配置文件

By adding an HttpHandler I assume you mean the configuration files

<system.web>
    <httpHandlers>...</httpHandler>
</system.web>

有一种方法可以通过在请求期间直接添加IHttpHandler来自动控制它.因此,在应用程序生命周期中的 PostMapRequestHandler 上,您应该执行以下操作,在您自己的自定义IHttpModule中:

There is a way to control it automatically, by adding the IHttpHandler in directly during the request. So on the PostMapRequestHandler in the Application Lifecycle, you would do the following, in your own custom IHttpModule:

private void context_PostMapRequestHandler(object sender, EventArgs e)
{
    HttpContext context = ((HttpApplication)sender).Context;
    IHttpHandler myHandler = new MyHandler();
    context.Handler = myHandler;
}

这将自动为该请求设置处理程序.显然,您可能希望将其包装在某种逻辑中,以检查诸如动词,请求url等内容.但这就是这样做的方式.这就是许多流行的URL Rewriters的工作量,例如:

And that would automatically set the handler for that request. Obviously you probably want to wrap this in some logic to check for things such as verb, requesting url, etc. But this is how it would be done. Also this is how many popular URL Rewriters work such as:

http://urlrewriter.codeplex.com

不幸的是,使用预先配置的配置处理程序.confi g可以,它被隐藏了,似乎无法访问.它基于名为IHttpHandlerFactory的接口.

Unfortunately though, using the pre built configuration handler that the web.config does, is hidden away and doesn't seem to be accessible. It is based off an interface called IHttpHandlerFactory.

更新 IHttpHandlerFactory可以像其他任何IHttpHandler一样使用,只是将其用作启动点而不是处理点.请参阅本文.

Update The IHttpHandlerFactory can be used just like any other IHttpHandler, only it is used as a launching point instead of a processing point. See this article.

http://www.uberasp.net/getarticle.aspx?id=49

这篇关于以任何方式在.NET中以编程方式添加HttpHandler吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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