如何覆盖 ASP.NET 中的页面加载生命周期以防止加载 ASPX 表单?
我需要在多个 ASPX 代码隐藏文件中测试某个条件,并且在某些情况下,希望完全绕过正常的页面加载过程,以便不加载相应的 ASPX 页面。 相反,我想向浏览器发送通过代码隐藏方法编写的自定义响应。
有谁知道从哪里开始 - 页面生命周期中要覆盖哪些方法以及确保在正常 ASPX 页面内容被抑制时将我的自定义 Response.Write 发送到浏览器的最佳技术?
谢谢。
I need to test a condition in several ASPX code-behind files and, in some cases, would like to completely bypass the normal page load process so that the corresponding ASPX page is not loaded. Intead, I'd like to send a custom response to the browser that's written from a code-behind method.
Does anyone know where to start- what method(s) in the page lifecycle to override and the best technique to ensure that my custom Response.Write is sent to the browser while the normal ASPX page content is suppressed?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能是最简单的方法 - 使用
Page_Load()
。Probably the easiest way to do it - use
Page_Load()
.使用 Response.End() 的“简单”方法对性能来说很糟糕,会抛出终止线程的异常。
http://blogs.msdn.com/b/tmarq/archive/2009/06/25/ Correct-use-of-system-web-httpresponse-redirect.aspx
http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx
我有同样的问题并以这种方式解决了。 这是一个两步过程:首先调用 HttpApplication.CompleteRequest() 并退出处理。 接下来重写 Render() 以便不调用基本方法。 示例代码则变为:
The "easy" way to do it, with Response.End() is terrible for performance, throwing an exception which terminates the thread.
http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx
http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx
I had the same question and solved it this way. It's a two-step process: First call HttpApplication.CompleteRequest() and exit your processing. Next override Render() so that the base method is not called. The example code then becomes:
这实际上取决于您要回复的内容,是发布的表单字段、身份验证信息等...?
使用 Page_Load 显示的方法将起作用,但页面生命周期中该点之前的任何内容也将执行。
It really depends what you're responding to, is it a posted Form field, authentication info etc...?
The method shown using Page_Load will work, but anything before that point in the page lifecycle will also execute.