3

I have 2 controllers Home with

public class HomeController : Controller
    {
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                // do some irrelevant stuff
                    base.OnActionExecuting(filterContext);           

            }

    public ActionResult Index()
            {            
                    return View();
            }
}

and Service with

public ActionResult Confirm()
            { return RedirectToAction("Index", "Home");}

And one ActionFilterAttribute with OnActionExecuting method

 public class InvitationModeAttribute : ActionFilterAttribute
    {
     public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
               // do some stuff

                base.OnActionExecuting(filterContext);
            }
}

public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {            
            filters.Add(new InvitationModeAttribute());
        }
    }

When I go to localhost/Service/Confirm , OnActionExecuting is fired, but then when RedirectToAction is called, OnActionExecuting is not fired. How can I catch this after RedirectToAction?

Thanks

6
  • Show the code of both controllers. Commented Jun 26, 2015 at 12:13
  • edited... the point is that OnActionExecuting is not fired after RedirectToAction Commented Jun 26, 2015 at 12:19
  • Where you have written your OnActionExecuting method ? Commented Jun 26, 2015 at 12:21
  • @Maarty I don't see attributes. have you decorated both actions with desired attribute? Commented Jun 26, 2015 at 12:24
  • @Kamo - nope, what attribute I need here? Commented Jun 26, 2015 at 12:27

1 Answer 1

1

Refer this For More clarity

First of all Remove OnActionExecuting method in controller level

public class HomeController : Controller
{
       [InvitationModeAttribute]
     public ActionResult Index()
     {            
        return View();
     }
 }

2nd Controller

 public class ServiceController : Controller
 {
   [InvitationModeAttribute]
   public ActionResult Confirm()
   { 
     return RedirectToAction("Index", "Home");
   }
 }

From MSDN

Scope of Action Filters

In addition to marking individual action methods with an action filter, you can mark a controller class as a whole with an action filter. In that case, the filter applies to all action methods of that controller. Additionally, if your controller derives from another controller, the base controller might have its own action-filter attributes. Likewise, if your controller overrides an action method from a base controller, the method might have its own action-filter attributes and those it inherits from the overridden action method. To make it easier to understand how action filters work together, action methods are grouped into scopes. A scope defines where the attribute applies, such as whether it marks a class or a method, and whether it marks a base class or a derived class.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Well it works. But it is not what I want.. Anyway I am definitely doing wrong approach.. What I need is to know in HomeController that it was being redirected from Service controller. I want it for all methods there, so I don't want to use any cookies or parameters for every single redirect. Is there any way to do it?
I am afraid you cant do it AFAIK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.