views:

109

answers:

6

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?

+2  A: 

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.

JcMalta
I kind of like it... how "heavy" is your logging (how much performance impact does it have?)
Chad
I think the overhead is pretty low .... I am currently using the NLog logging utility (http://nlog-project.org/) for actual logging. Previsously I have also used log4net (http://logging.apache.org/log4net/index.html) with success.
JcMalta
+2  A: 

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.

Ryan
as for me you should use model binders for such a thing.
Sergey Mirvoda
@Sergey, this is on the way out to the view, not on the way in.
Ryan
+1  A: 

Non-model state validation filter

This is an everyday scenario of creating unique records in the DB. Suppose you have an entity User. You have all the possible validation attributes set on it, but there's one you can't put on it. And that is whether the entity instance is unique.

Why do we need one anyway?
These are two most common processes with User entity where validation is involved:

  1. Creating new users
  2. Updating user's data

So then you create a new user, you have to most probably check whether it's unique in your DB (either username or email or something similar; something has to be checked for uniqueness).

But when you do an update, uniqueness shouldn't be checked, because the user already exists in the DB.

How do we solve this?

By using an action filter on the Create action. But since this kind of filter should be used will all different kinds of entities it's a wise thing to make it more generic and reusable, so we can actually use it will all kinds of entities.

This is how I've done it.

Robert Koritnik
This sounds like it would be better suited to a service layer. Technically MVC is your UI layer only.
Ryan
+2  A: 

reCaptcha validation filter

Any public facing web site most certainly needs some sort of captcha human validation. So why not use the one that's very strong and has very good purpose behind it as well. reCaptcha.

Integrating with MVC is rather easy.

Robert Koritnik
+2  A: 

Unsupported Browser Filter(UBF) - checks Request.Browser and redirects to Browser Download Page or lightweight page.

By default all our controllers supports only browsers which jQuery support. But programmer van mark controller with special attribute for overriding UBF

MVC3 new global filters makes use of UBF even more easy.

Sergey Mirvoda
Ryan
Yes I know, we developed our own one. vote for NIH principle!:) Thanks for links IJoinedFilter infrastructure is very close to our own implementation.
Sergey Mirvoda
A: 

I've got two I can't live without:

a) AjaxMasterPageInjectorAttribute : this little guy's job is to check if the request IsAjax() and then swap to the chromeless AjaxMasterPage as appropriate. Corolarry is the JsonCommandInterceptor--it takes the response and changes it to a Json command for the ajax scenarios as required. Allows one to make a single action chain that dynamically becomes ajaxy if you need it to.

b) ViewModelWrapperInjectorAttribute : grabs the view model and inserts into a more global site-wide ViewModelWrapper of some sort. Keeps your actions focused on handling what they should and fobs off the business of populating/providing said ViewModelWrapper to a more infrastructurey place.

Wyatt Barnett