Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify a model in an ActionFilter

I have an actionfilter that I am running OnActionExecuting in ASP.NET MVC 2. Essentially I would like the actionfilter to sanitize my data and replace the current model (which will be passed to subsequent action filters and also my action method) with the sanitized model. Is this possible and is it a bad idea - if so why?

Thank you in advance, JP

like image 866
JP. Avatar asked Jan 12 '10 04:01

JP.


1 Answers

If you need to deal with your models, you're likely going to be dealing more within the scope of a single Controller (unless all your Controllers use the same model types?). An alternate approach would be to override the OnActionExecuting() and OnActionExecuted() methods of the Controllers themselves. This allows you to keep your business logic within the controller scope.

Generally ActionFilters are used for cross-cutting concerns - something that you want to run for many action methods, regardless of where they exist in the app. So unless your model sanitization logic applies across many controllers and actions, or is very generic (which perhaps it is, in which case your approach is probably good), you might want to bring it out of the filters and into your controllers. If it's something that can apply broadly, then an ActionFilter is just fine.

like image 79
womp Avatar answered Sep 20 '22 10:09

womp