I have been trying to use the Response.End() method and I keep on getting this error;
[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
I’ve googled this and from what I’ve seen there are 2 solutions, one being to use HttpContext.Current.ApplicationInstance.CompleteRequest(); and the other is to just catch the exception and do nothing.
Firstly the ‘catch and do nothing’ seems like a very ‘bad’ way of doing this, surely we should actually handle the exception or do something so that the exception doesn’t occur? Surely that’s good practice? I don’t want my code to be filled with lots of try-catches.
The other method HttpContext.Current.ApplicationInstance.CompleteRequest(); Doesn’t seem to be working.
Here is a snippet of my code:
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "text/html");
Response.Write("Blocked IP");
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
All I want to do is to output the text ‘Blocked IP’ but with using the HttpContext.Current.ApplicationInstance.CompleteRequest(); method the rest of the aspx page gets printed under it like this:
Blocked IP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form method="post" action="Email.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZIELtfylTwZMU8vGWe9UrhWClqN3i4rMTvAp8otze+6G" />
</div>
<div>
</div>
</form>
</body>
</html>
This is intended. It is designed to throw this exception so asp.net knows to stop the current execution.
From HttpResponse.End.
It is indended to be used where lets say
Reponse.End()is called withinPage_Init, it doesn’t let ASP.NET callPage_Loadand the rest of the events in the page lifecycle.