Snake Eyes
Snake Eyes

Reputation: 16764

Force redirect from attribute in ASP.NET MVC before reach a section from controller in ASP.NET MVC

Might be a simple question. Please let me show you what is my problem.

I have a custom attribute like

public class MyCustomAttribute: ActionFilterAttribute
{
   public override void OnActionExecuting( ActionExecutingContext filterContext )
      {
        if(somethingTrue) {
           filterContext.Result = new RedirectToRouteResult ( ... );
        }
     }
}

and my controller class

[MyCustom]
public class ContactController: Controller 
{
    protected override void OnResultExecuting( ResultExecutingContext filterContext )
    {
        // so something
    }
}

If I put breakpoint to OnResultExecuting method, it is reached even I put an attribute in top of controller class.

I expected that won't reach OnResultExecuting method from controller because I create a redirection result.

Or is my problem that I don't understand correctly the attribute ?

Upvotes: 0

Views: 554

Answers (1)

Neel
Neel

Reputation: 11741

Well i guess you want to skip OnResultExecuting so i would prefer to write below code :-

 public override void OnActionExecuting( ActionExecutingContext filterContext)
      {

        if (true)
        {
            //Create your result
            filterContext.Result = new EmptyResult();
        }
        else
            base.OnActionExecuting(filterContext);
    }

Upvotes: 1

Related Questions