asp.net is invalid using Response.Redirect try{}catch{} block

Source: Internet
Author: User
Tags exception handling
Try 

{ 

...

.. Response.Redirect ("/mymaimai.aspx"); ..

} 

catch (Exception e)

{ 

//exception handling

}

Using the above statement, regardless of whether there is an exception, the catch will be executed, always show "" failed ", will throw System.Threading.ThreadAbortException, for the following reasons:

The Response.End method stops the execution of the page and transforms the execution to the Application_EndRequest event in the application's event pipeline. The code line following the Response.End is not executed.

This problem occurs in the Response.Redirect and Server.Transfer methods because both of these methods call Response.End internally.

Solution

To resolve this issue, use one of the following methods:

For Response.End, call the Applicationinstance.completerequest method without invoking the Response.End so that the code execution of the Application_EndRequest event is skipped.

For Response.Redirect, use the overloaded Response.Redirect (String URL, bool endresponse) to pass false to the Endresponse parameter to cancel the inside of the Response.End Call. For example:

Response.Redirect ("/mymaimai.aspx", false); If you use this workaround, the code behind Response.Redirect will be executed.

For Server.Transfer, use the Server.Execute method instead.

State

This phenomenon is the result of design.

Resolved code:

Try 

{ 

Response.Redirect ("/mymaimai.aspx", false); 

} 

Catch 

{ 

//exception handling

}
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.