ASP. NET page Jump response. Redirect throws an exception

Source: Internet
Author: User

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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.