views:

18

answers:

2

Hello,

I've this action filter that executes before all the action methods executes.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
  //code omitted 
  UpdateModel(MyModel);
}

I want this action to update the model when actions A, B, C are supposed to execute, not D. How do I prevent this filter action from updating the model when D will execute? So is it possible to know using the ActionExecutingContent filterContext above?

Thanks

+1  A: 
filterContext.ActionDescriptor.ActionName
Raj Kaimal
Although this answers the OP:s question, it really would be bad practice to limit an attribute's usage depending on the name of the action it's applied to. What if someone else comes to look at this code in the future, and wants (and expects) the actionfilter to run on *all* actions?
Tomas Lycken
Thanks for both answers.(See comments I wrote to Tomas Lycken)
Richard77
+1  A: 

You do it by decorating actions A, B, and C with the attribute, but not D.

You have recieved an answer, but it would be really bad practice to this the way you intend. What if someone else writes an action method in another controller where this attribute should run, and then, by chance, chooses a name that you have filtered away. The attribute won't run - a clear bug - but it will do exactly as it is told. It will be a maintainence nightmare.

No, the clean way to do this would be to move the application of the attribute from controller level to action method level for all controllers where there are action methods that shouldn't have this attribute. That makes it easy to see, just by looking at the controller code, when the attribute will run and when not., and it will work just as expected.

Tomas Lycken
with which attribute?
Richard77
@Richard, The attribute you're writing, that contains the method in your question. Doing it any other way would be very bad practice, see my update.
Tomas Lycken
Thanks for both answers. You guys are right. In fact, I was trying do dodge an other problem. Anyway, I solved a problem but I introduced many others. For instance, each time I skipped updating the model, I lost my data, etc. So, again, thanks for the answers (...)
Richard77