Remember to return to Emptyresult after using the Response.Redirect method in MVC

Source: Internet
Author: User

In the ASP. In MVC we often use the Response.Redirect method to jump directly in interceptors and controllers, but after the Response.Redirect method is actually executed, ASP. NET does not immediately end the execution of the current request, but it will take some time before the current request is terminated. Executes, and then commands the client browser to access the new URL address that is passed in the Response.Redirect method. This can cause a problem, Sometimes we want the code to be executed after the Response.Redirect method executes, because this is not the behavior we expected, and after the code executes the Response.Redirect method, if some of the code behind it continues to execute, it may even make an error.

For example, in the following code we demonstrate that in the MVC Iauthorizationfilter interceptor, if the user is not logged in should immediately stop the current page of the request, and then jump to the login page. We used the Response.Redirect method to do the jump.

1  Public classAuthenticationfilterattribute:actionfilterattribute,iauthorizationfilter,iactionfilter2     {3         #regionIauthorizationfilter Members4 5          Public voidonauthorization (AuthorizationContext filtercontext)6         {7 8             stringCuractionname =FilterContext.ActionDescriptor.ActionName;9             stringCurcontrollername =FilterContext.ActionDescriptor.ControllerDescriptor.ControllerName;Ten  One             if(!filtercontext.httpcontext.request.isauthenticated && (Curcontrollername.tolower ()! ="Login"|| Curactionname.tolower ()! ="Index")) A             { -FilterContext.HttpContext.Response.Redirect (Loginhelper.loginpageurl,true); -                 return; the             } -         } -  -         #endregion +}

I was surprised to find that even though the code was response.redirect on line 13 above, ASP. NET executed the code in the action of the current request URL corresponding to the controller, and even executed the Razor engine Code of the view that the action returned ... Although the content of this view is not presented to the client browser at the end, the browser finally jumps to the login page correctly. But based on the debugs we found that even after we executed the Response.Redirect method, ASP . NET executed a lot of code that should not be executed.

So this time we need to use the Emptyresult object, and we'll change the code above to look like this:

1  Public classAuthenticationfilterattribute:actionfilterattribute,iauthorizationfilter,iactionfilter2     {3         #regionIauthorizationfilter Members4 5          Public voidonauthorization (AuthorizationContext filtercontext)6         {7 8             stringCuractionname =FilterContext.ActionDescriptor.ActionName;9             stringCurcontrollername =FilterContext.ActionDescriptor.ControllerDescriptor.ControllerName;Ten  One             if(!filtercontext.httpcontext.request.isauthenticated && (Curcontrollername.tolower ()! ="Login"|| Curactionname.tolower ()! ="Index")) A             { -FilterContext.HttpContext.Response.Redirect (Loginhelper.loginpageurl,true); -Filtercontext.result =NewEmptyresult ();//join Emptyresult to tell ASP. NET MVC does not have to execute the code of the Controller action for the current request after the interceptor execution ends the                 return; -             } -         } -  +         #endregion -}

So when ASP. NET MVC executes the above Iauthorizationfilter interceptor, it will find that Emptyresult is assigned to the result property of the parameter Filtercontext. The action of the controller is terminated, the execution of the current request is immediately ended, and no extra code is executed.

So very often when we use the Response.Redirect method in ASP to be quite careful, it is important to know that the Response.Redirect method does not mean that the code to end execution, but also to consider the subsequent code once the error will have what the consequences, there is no way to avoid. Using Emptyresult in MVC is a good choice.

Remember to return to Emptyresult after using the Response.Redirect method in MVC

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.