Search code examples
asp.net-mvc-3action-filter

I want to execute code when every action is called in ASP.NET MVC3


I'm new to MVC3, and my site has a setting that if the site is open. So, I need to check this value when every action is called.

The idea is like:

public ActionResult SomeAction()
{
    if(!settings.SiteIsOpen)
    {
        // redirect to siteclosed page
    }
    // someaction logic...
}

I know that there are something called Filter in MVC3, and need to register it RegisterGlobalFilters. I want to use this feature, but I don't know the detail steps to implement that.

Can anyone help me? thanks!


Solution

  • Take a look at the ActionFilterAttribute. It should get you started on where you want to go. You'll just need to figure out where in the request you want to apply your action.