任何一种添加的HttpHandler编程在.NET? [英] Any way to add HttpHandler programatically in .NET?

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

问题描述

我一直在研究这个有点而是跨越一个答案还没来 - 有什么办法可以编程一个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 直接。所以在波斯特马prequestHandler在应用程序生命周期,你会做以下,自定义的 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.$c$cplex.com

可惜的是,使用 pre内置配置处理器的web.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.

<一个href=\"http://www.uberasp.net/getarticle.aspx?id=49\">http://www.uberasp.net/getarticle.aspx?id=49

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

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