ASP.NET MVC : Response.Redirect(url, TRUE) does not stop request processing

前端 未结 5 1423
清歌不尽
清歌不尽 2020-12-16 15:43

I have a method decorated with two custom ActionFilterAttribute.

[RequiresAuthentication(Order = 1)]
[ToonAction(Order = 2)]
public ActionResult Browse(...
         


        
相关标签:
5条回答
  • 2020-12-16 16:04

    You want to set the Result on the filterContext to a RedirectResult, not do a redirect on the response.

     filterContext.Result = new RedirectResult { Url = loginUrl };
    

    EDIT: As @Hunter Daley suggests a better mechanism would be to use the AuthorizeAttribute instead if it works for you. If you do have authentication/authorization scenarios that the AuthorizeAttribute doesn't work for, it would probably be better to derive your custom attribute from it instead of the more generic ActionFilterAttribute. In any event, the correct technique is to set the Result rather than interact with the Response directly. You might want to look at the actual AuthorizeAttribute source at http://www.codeplex.com/aspnet for ideas.

    I've got a sample of custom authorization code on my blog, http://farm-fresh-code.blogspot.com, too.

    0 讨论(0)
  • 2020-12-16 16:04

    Add

    filterContext.HttpContext.Response.Clear();
    

    at first and this at End :

    filterContext.HttpContext.Response.End();
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-16 16:05

    Add this code before you redirect the page.

    filterContext.ExceptionHandled = true;
    
    0 讨论(0)
  • 2020-12-16 16:08

    try adding the [Authorize] attribute to your Action methods instead

    0 讨论(0)
  • 2020-12-16 16:20

    you can use return RedirectToAction("Index", "Home/Login", new {area = "", returnURL = Request.Url.AbsolutePath}); to stop the current processing, redirect to the desired (login) page and quit the action. the area route property is needed to get out of the current area if you are in any.

    0 讨论(0)
提交回复
热议问题