ASP.NET HttpModule - 保证执行处理程序前和处理后代码?
基本上,我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了不调用 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.
将其添加到您的 Global.asax 文件中:
这应该 100% 有效。
Add this to your Global.asax file:
That should work 100%.