When you jump to other pages in the ASP. NET button response, an exception 'thread was being aborted' is thrown. The following code
Try
{
String redirecturl = "~ /Default. aspx ";
Response. Redirect (redirecturl );
}
Catch (system. Exception ex)
{
}
Find the corresponding problem on the Microsoft Site:
Threadabortexception occurs if you use response. End, response. Redirect, or server. Transfer
According to Microsoft's solution, you only need to callResponse. RedirectOverload FunctionsResponse. Redirect (string URL, bool endresponse)In this example, the endresponse parameter is set to false.
Response.Redirect(redirectUrl,false);
After reading the source code of ASP. NET, an exception is thrown. The following code is used:
/// <devdoc> /// <para>Redirects a client to a new URL.</para> /// </devdoc> public void Redirect(String url, bool endResponse) { if (url == null) throw new ArgumentNullException("url"); if (url.IndexOf('\n') >= 0) throw new ArgumentException(SR.GetString(SR.Cannot_redirect_to_newline)); if (_headersWritten) throw new HttpException(SR.GetString(SR.Cannot_redirect_after_headers_sent)); Page page = _context.Handler as Page; if ((page != null) && page.IsCallback) { throw new ApplicationException(SR.GetString(SR.Redirect_not_allowed_in_callback)); } url = ApplyRedirectQueryStringIfRequired(url); url = ApplyAppPathModifier(url); url = ConvertToFullyQualifiedRedirectUrlIfRequired(url); url = UrlEncodeRedirect(url); Clear(); // If it's a Page and SmartNavigation is on, return a short script // to perform the redirect instead of returning a 302 (bugs ASURT 82331/86782)#pragma warning disable 0618 // To avoid SmartNavigation deprecation warning if (page != null && page.IsPostBack && page.SmartNavigation && (Request["__smartNavPostBack"] == "true")) {#pragma warning restore 0618 Write("<BODY><ASP_SMARTNAV_RDIR url=\""); Write(HttpUtility.HtmlEncode(url)); Write("\"></ASP_SMARTNAV_RDIR>"); Write("</BODY>"); } else { this.StatusCode = 302; RedirectLocation = url; Write("
The above red code thread. currentthread. Abort will throw an exception, as long as the execution still allows the cancel time to throw an exception.
Of course, when will it not be allowed? (_ context. isincancellableperiod is false). I did not check it.