How do I get certain code to execute before every single controller action in ASP.NET MVC 2?

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-16T02:48:11Z Indexed on 2010/04/16 2:53 UTC
Read the original article Hit count: 184

Filed under:

I want to check some things about the state of the session, the user agent, etc, and possibly take action and return a special view BEFORE a controller method gets a chance to execute. For example:

Most common:
User requests Home/Index
System checks to make sure x != 0.
x does not equal zero, so the Home/Index controller executes like normal.

But, sometimes:
User requests Home/Index
System checks to make sure x != 0.
x DOES equal zero. The user must be notified and the requested controller action cannot be allowed to execute.

I think this involves the use of ActionFilters. But I have read about them and I don't understand if I can preempt the controller method and return a view before it executes. I am sure I could execute code before the controller method runs, but how do I keep it from running in some instances and return a custom view, or direct to a different controller method?

© Stack Overflow or respective owner

Related posts about asp.net-mvc