How can I make a function to run on all of the controllers in ASP.NET MVC 2? -
i have function :
- gets information model ( done )
- gets information cookie ( done ), ,
- set new informations on viewdata ( on views ) on every controller
also, function need run on every controller when controller calling (i don`t know how this).
i have write function on basecontroller error:
object reference not set instance of object.
and, think not right way. i'm using asp.net mvc 2 , .net 3.5.
thx help.
create custom action filter:
public class myactionfilter : actionfilterattribute { public override void onactionexecuted(actionexecutedcontext filtercontext) { // if actionresult not viewresult (e.g jsonresult, contentresult), // there no viewdata don't anything. var viewresult = filtercontext.result viewresult; if (viewresult != null) { // call function, whatever want result, e.g: viewresult.viewdata["somekey"] = somedata; } } }
slap bad boy on base controller:
[myactionfilter] public class basecontroller : controller { }
now, after every actionresult every controller executed, action filter logic executed.
you've got few other events can hook into, sounds want stuff after action has been executed, think above should suit fine.
Comments
Post a Comment