Trying to follow best practice for Response.Redirect
Response.Redirect(someURl, false);
Context.ApplicationInstance.CompleteRequest();
As described in this MSDN blog: correct-use-of-system-web-httpresponse-redirect Which basically says:
Use the CompleteRequest overload that does not cause a thread abort exception.
The MSDN page for CompleteRequest says:
Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
However all page events after calling CompleteRequest still fire. So no code is skipped. This appears to contradict the MSDN documentation. Is there a workaround? Have i misunderstood the documentation?
(The blog post does say the skip is at page level but the API documentation does not say this. I'm guessing the answer will just be the documentation is misleading.)
You can test by debug stepping quite a simple page:
<asp:CheckBox ID="CheckBox0" runat="server" OnCheckedChanged="chk_CheckedChanged" />
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="chk_CheckedChanged" />
<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="chk_CheckedChanged" />
<asp:CheckBox ID="CheckBox3" runat="server" OnCheckedChanged="chk_CheckedChanged" />
<asp:Button ID="btnRedirect" runat="server" Text="Click Me" OnClick="btnRedirect_Click" />
<asp:Label ID="lblNotes" runat="server" />
Code:
protected void Page_Load(object sender, EventArgs e)
{
lblNotes.Text += " Load " + DateTime.Now.ToString();
}
protected void Page_PreRender(object sender, EventArgs e)
{
lblNotes.Text += " PreRender " + DateTime.Now.ToString();
}
protected void chk_CheckedChanged(object sender, EventArgs e)
{
Response.Redirect("stackoverflow.com", false);
Context.ApplicationInstance.CompleteRequest();
}
protected void btnRedirect_Click(object sender, EventArgs e)
{
lblNotes.Text += " Btn " + DateTime.Now.ToString();
}
If you check all the tick boxes then press the button many events are fired.