For the bugs in Response. Redirect and RedirectToAction in ASP. net mvc (after the jump, continue to execute the following code without terminating the process) and the handling method,

Source: Internet
Author: User

For the bugs in Response. Redirect and RedirectToAction in ASP. net mvc (after the jump, continue to execute the following code without terminating the process) and the handling method,

In traditional ASP. NET, you can use Response. Redirect ("") to directly Redirect the page and end the current process.

However, in MVC, I do not know whether Microsoft intends to fix it or it is a BUG (for example, @ Html. dropDownList Name BUG) when using Response. the execution of the current page will not be interrupted when Redirect performs page redirection. This is not in line with expectations and will have unpredictable consequences, even if the parameter is added with true, it will also be invalid.

Public ActionResult RedirectError () {if (true) // For example, if the verification is successful, go to the background homepage {Response. redirect ("http://www.chengchenxu.com", true);} int a = 7-7; int B = 7/a; // create a runtime exception, access to this page directly produces an exception, and it is difficult to jump. return View ();}

 

 

If the above Code is in an Action and the access time is inconsistent with the expectation, it will continue to be executed and generate exceptions without redirection. This is a BUG.

Now let's try it with RedirectToAction (). The result is the same.

 

 

 

The solution is as follows: Create an EmpeyResult and return it. The Code is as follows:

If (true) // For example, if login verification is successful, go to the background homepage {Response. redirect ("http://www.chengchenxu.com", true); EmptyResult er = new EmptyResult (); return View (er);} int a = 7-7; int B = 7/; // create a running exception. Access to this page will cause an exception and it is difficult to jump. return View ();

 

In this way, there will be no problem. I don't know if my usage is incorrect or what is the problem. In general sense, after the jump action is executed, the current page should be interrupted to execute a new page, in MVC, this place is confusing. Fortunately, there is a solution and it is relatively simple.

 

Download sample code:

ChengChenXu.com.RedirectTest.rar

 

This article is original to the blogger. for reprinting, please keep the source:
Http://www.chengchenxu.com/Article/Show/17/redirect

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.