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