ASP.NET HttpModule - 保证执行处理程序前和处理后代码?

发布于 2024-07-08 05:17:12 字数 435 浏览 5 评论 0原文

基本上,我试图在 ASP.NET HttpModule 中编写以下内容(伪代码):

*pre-code*
try { handler.ProcessRequest(...) }
catch (Exception) { *error-code* }
finally { *post-code* }

我发现我可以挂钩 HttpModule.PreExecuteHandler 来表示“前代码”,并挂钩 .Error 来表示“错误代码”。 但 PostExecuteHandler 似乎运行不可靠。

BeginRequest 和 EndRequest 运行可靠,但对于我需要编写的代码来说还为时过早,这需要检查选择执行的处理程序。 直到 BeginRequest 之后才会选择处理程序。

编写这种包装器是否有最佳实践?

谢谢!

Basically, I'm trying to write the following (pseudocode) in an ASP.NET HttpModule:

*pre-code*
try { handler.ProcessRequest(...) }
catch (Exception) { *error-code* }
finally { *post-code* }

I've found that I can hook into HttpModule.PreExecuteHandler for "pre-code" and .Error for "error-code". But PostExecuteHandler doesn't seem to be running reliably.

BeginRequest and EndRequest run reliably but are too early for the code I need to write, which requires inspection of the handler that was chosen to execute. The handler isn't chosen until after BeginRequest.

Is there a best practice for writing this kind of wrapper?

Thanks!

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

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

发布评论

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

评论(2

稍尽春風 2024-07-15 05:17:12

除了不调用 Response.End 之外,没有办法做你想做的事(至少在 HttpModule 中)。 这篇文章很好地解释了它,并提供了 Response.End 的替代方案如果这是您调用 Server.Transfer 的副作用。

There is no way to do what you want (in a HttpModule, at least), other than to not call Response.End. This article explains it pretty well and offers an alternative to Response.End in case it is a side-effect of your having called Server.Transfer.

涙—继续流 2024-07-15 05:17:12

将其添加到您的 Global.asax 文件中:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    //
}

protected void Application_PostRequestHandlerExecute(object sender, EventArgs e)
{
    //
}

这应该 100% 有效。

Add this to your Global.asax file:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    //
}

protected void Application_PostRequestHandlerExecute(object sender, EventArgs e)
{
    //
}

That should work 100%.

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