我需要在几个ASPX后台代码文件和测试一个条件,在某些情况下,想要完全绕过正常页面加载过程,以便相应的ASPX页面没有加载。大家,我想发送一个自定义响应浏览器年代从后台代码的编写方法。
有谁知道从哪里开始,什么方法(s)在页面生命周期覆盖和最好的技术来确保我的自定义响应。写被发送到浏览器,而正常的ASPX页面内容是镇压?
谢谢。
我需要在几个ASPX后台代码文件和测试一个条件,在某些情况下,想要完全绕过正常页面加载过程,以便相应的ASPX页面没有加载。大家,我想发送一个自定义响应浏览器年代从后台代码的编写方法。
有谁知道从哪里开始,什么方法(s)在页面生命周期覆盖和最好的技术来确保我的自定义响应。写被发送到浏览器,而正常的ASPX页面内容是镇压?
谢谢。
可能最简单的方法——使用<代码> employee() > < /代码。
protected void Page_Load(object sender, EventArgs e)
{
bool customResponse = true;
if (customResponse)
{
Response.Write("I am sending a custom response");
Response.End(); //this is what keeps it from continuing on...
}
}
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
我有同样的问题,这样解决。这年代一个两步过程:首先调用HttpApplication.CompleteRequest()和退出你的处理。接下来覆盖渲染(),基本方法是不叫的。示例代码就变成了:
bool customResponse = true; protected void Page_Load(object sender, EventArgs e) { if (customResponse) { Response.Write("I am sending a custom response"); this.Context.ApplicationInstance.CompleteRequest(); return; // Bypass normal processing. } // Normal processing... } protected override void Render(HtmlTextWriter writer) { if (!customResponse) base.Render(writer); // Then write the page as usual. }
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.