拦截 Page 对象创建以挂钩事件

发布于 2024-09-18 22:37:21 字数 380 浏览 5 评论 0原文

我正在寻找一种方法来拦截 ASP.NET 处理管道,以便能够将事件处理程序注册到 Page 类上的所有事件。原因是,我需要维护一个组件的会话绑定实例,该实例需要从 OnPreInit 开始通知所有重要的 Page 事件。

应用程序中有任意数量的 Page 后代,这些后代不受我的控制。因此,我无法使用像使用单个自定义后代这样的方法,该方法会通知会话绑定组件,作为 Web 应用程序中所有页面的基类。

我不认为创建自定义 IHttpHandlerIHttpModule 实现可以解决问题。另请注意,我无法创建自定义 HttpApplication 后代。

I'm looking for a way to intercept the ASP.NET processing pipeline in such a way to be able to register event handlers to all events on the Page class. The reason is, I need to maintain a session-bound instance of a component that needs to be notified of all important Page events, starting from OnPreInit.

There's an arbitrary number of Page descendants in the application, which are not under my control. Hence I cannot use an approach like using a single custom descendant, that would notify the session-bound component, as a base class for all pages in the web application.

I don't think creating a custom IHttpHandler or IHttpModule implementation would solve the problem. Also note I cannot create a custom HttpApplication descendant.

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

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

发布评论

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

评论(2

ぺ禁宫浮华殁 2024-09-25 22:37:21

执行您正在查看的操作不会是一个优雅的过程,特别是如果您需要处理多个页面事件,但理论上完全可以从 Global.asax 内部设置每个页面所需的处理程序每一页。

这里的技巧是将您的代码添加到 PreRequestHandlerExecute 方法中的 global.asax 中,从这里您可以访问 HttpApplication 对象,从那里访问页面,然后注册您的事件。此过程是必要的,因为会为每个处理的页面创建一个新的页面实例。

现在,您知道的其他选项要优雅得多,但这应该可以满足您的需要。我发现一个有用的教程,虽然围绕主题向您展示了整个过程此处

编辑:

看到您的评论后,是的,您可以在自定义 HttpModule 中简单地执行我上面所述的操作。我链接的文章甚至向您展示了这个过程:)

It isn't going to be an elegant process to do what you are looking at, especially if you need to handle multiple page events, but in theory it is fully possible from within the Global.asax to setup handlers that you need for each and every page.

The trick here is to add your code to the global.asax in the PreRequestHandlerExecute method, from here you can get access to the HttpApplication object, get access to the page from there, and then register your events. This process is necessary as a new page instance is created for every page that is processed.

Now, other options as you know are far more elegant, but this should get to where you need to be. One helpful tutorial I found although around Themeing shows you the whole process here.

EDIT:

After seeing your comment, yes, you can simply do what I'm stating above, in a custom HttpModule. The article I linked even shows you that process :)

云醉月微眠 2024-09-25 22:37:21

如果不了解更多关于您想要完成的任务,听起来您确实想要创建一个 http 模块或处理程序。您可能想看看 这个问题

Without knowing more about what you're trying to accomplish it really sounds like you do indeed want to create a http module or handler. You might want to take a look at this question

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