views:

42

answers:

2

Hi,

I'm trying to execute a Redirect from a method attribute. It seems to work:

public class MyAttribute: ActionFilterAttribute {
    [..]
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        [..]
        filterContext.HttpContext.Response.Redirect(urlToRedirectTo, true);
        [..]

The only problem is that the redirect is executed after the end of the method it's attached to, while i'd like the redirect to prevent the execution of the method.

Any help? Thanks

A: 

You need to add

filterContext.HttpContext.Response.End();

EDIT: The top is wrong the bottom is right

this should work

filterContext.Result = new RedirectResult(UrlToRedirectTo);

if you return an ActionResult then it gets executed instaed of the action itself so this should skip the action.

Sruly
I think he ends the response by putting `true` as a second parameter in `HttpResponse.Redirect` method..
ŁukaszW.pl
+3  A: 

You can prevent execution of the action method by assigning an ActionResult to filterContext.Result. For example:

filterContext.Result = new RedirectResult(urlToRedirectTo);
ŁukaszW.pl
Thanks for accept ;) Regards..
ŁukaszW.pl