I need to run a certain function in every ActionResult and return a certain value if necessary.
Let’s say this function’s name is A(). I can do this manually:
ActionResult Index() {
if (...) return A();
...
}
ActionResult About() {
if (...) return A();
}
Or I can use the Initialize() method:
override void Initialize(RequestContext r) {
A(); // Can't do a return here
}
But the problem is that I can’t return an ActionResult value since it’s void.
Is there any way to do this?
You can do this with Action Filters.
Then you can add
[MyActionFilter]to controllers and/or actions