HttpHandler 和 HttpHandlerFactories

发布于 2024-07-29 03:50:16 字数 331 浏览 3 评论 0原文

我想路由不同资源的传入请求,一些返回文件(例如 css),另一些返回服务器生成的响应,另一些则重定向到 aspx 页面以实现 AJAX 功能。

当前配置使用 HttpModule 将所有请求重新映射到一个处理程序,在该处理程序中解析和分派 url,作为服务器的入口点。

我想知道处理请求的最佳配置是什么,我应该从 HttpModule 重新映射到 HandlerFactory,在其中解析 url,并根据该 url 路由到适当的处理程序,或者我应该尝试将其全部设置为网络配置?

另外,如何将请求从 HttpHandler 和 HttpHandlerFactory 路由到 aspx 页面?

I would like to route incoming requests for different resources, some returning files such as css, others returning responses generated by the server, and others being redirected to aspx pages for AJAX functionality.

The current configuration uses an HttpModule to remap all requests to one handler, where urls are parsed and dispatched, as an entry point to the server.

I am wondering what the best configuration to handle requests would be, should I remap from the HttpModule to a HandlerFactory, where I parse the url, and route to an appropriate handler based on that url, or should I attempt to set it up all in the web.config?

Also How can I route requests to an aspx page from a HttpHandler and HttpHandlerFactory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半夏半凉 2024-08-05 03:50:16

如果将请求路由到各种处理程序的规则非常简单(例如“.ext 转到处理程序 xyz”),那么您绝对应该使用带有 web.config 的内置 .NET 机制 - 没有理由在这里重新发明轮子。 如果逻辑可能更复杂或处理的不仅仅是扩展映射,则模块是合适的。

其次,您应该使用 HttpHandlerFactory,因为工厂可以返回 IHttpHandler,而 ASPX 页面就是一个 IHttpHandler。 因此,如果您使用自定义工厂,如果您的逻辑确定它应该“路由”到 ASPX 页面,您可以 直接从工厂返回页面的实例

IHttpHandler thePage = PageParser.GetCompiledPageInstance(
    requestPath,
    pathToAspxFile,
    httpContext);

return thePage;

If the rules for routing requests to various handlers is very simple (e.g. ".ext goes to handler xyz") you should definitely use the built-in .NET mechanism with web.config - there's no reason to reinvent the wheel here. If the logic is possibly more complex or deals with more than just extension mappings, a module is appropriate.

Secondly, you should use an HttpHandlerFactory, because a factory can return IHttpHandlers, and an ASPX page is an IHttpHandler. So if you use your custom factory, if your logic determines it should "route" to an ASPX page, you can return an instance of the page directly from the Factory:

IHttpHandler thePage = PageParser.GetCompiledPageInstance(
    requestPath,
    pathToAspxFile,
    httpContext);

return thePage;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文