I'm still not totally clear on why I would need to build custom action filters. Maybe a couple examples would help.
Are there any action filters in your project that you feel are a must-have? Maybe even so important that you re-use them across all your MVC projects?
Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns. Result filters contain logic that is executed before and after a view result is executed.
As Global Filter You need to add your filter globally, to add your filter to the global filter. You first need to add it on Global. asax file. You can use FilterConfig.
I use a "Logging" Action Filter to log all calls to my controllers with a dump of the parameters - this can be very useful during third-party testing allowing me to see how/why/when people are interacting with the application.
Although not an Action Filter, I also place a logging hook into my repositories that dumps the SQL generated by any Linq2SQL code ... again useful to see exactly what is being executed and when.
My favorite "must have" filter that I use is one that inspects the view model to see if there are any null lists. If there are, it tries to fill them from the database. I primarily use this for populating shared drop down lists so I don't have to put that code in the controller action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With