asp.net mvc - Best Way to reuse code inside my Controller action method -


i have code in same controller class looks same, such setting viewbags populate drop down lists, same code applies in post , create , edit action methods. have created private method @ end of controller class follow:-

private void populateviewbags() {     string controllername = routedata.values["controller"].tostring();     viewbag.possibledatacenters = repository.alldatacenter().orderby(a => a.name).tolist();     viewbag.possiblezones = repository.allzone().orderby(a => a.name).tolist();     list<string> s = new list<string>();     s.add(controllername.tolower());     viewbag.products = repository.getproducts(s).orderby(a => a.componentname).tolist();     viewbag.sites = repository.getsdorg().orderby(a => a.name).tolist();     viewbag.customers = repository.findaccountdefinition(null).tolist(); } 

and calling method inside action method. right way re-use code? thanks

there 2 standard ways can this.

1st approach - override onactionexecuting and/or onactionexecuted methods of controller class:

public class homecontroller: controller {     protected override void onactionexecuting(actionexecutingcontext filtercontext)     {         string controllername = routedata.values["controller"].tostring();         viewbag.controllername = controllername;     }      protected override void onactionexecuted(actionexecutedcontext filtercontext)     {         string controllername = routedata.values["controller"].tostring();         viewbag.controllername = controllername;     } } 

you can make abstract base controller implements methods , inherit concrete controllers abstract one, don't duplicate code code in each controller.

2nd approach - make custom actionfilter attribute , decorate each controller needs perform additional actions.

public class myactionfilterattribute: actionfilterattribute {     public override void onactionexecuting(actionexecutingcontext filtercontext)     {         string controllername = filtercontext.routedata.values["controller"].tostring();         filtercontext.controller.viewbag.controllername = controllername;     }      public override void onactionexecuted(actionexecutedcontext filtercontext)     {         string controllername = filtercontext.routedata.values["controller"].tostring();         filtercontext.controller.viewbag.controllername = controllername;     } } 

then decorate controllers, like:

[myactionfilter] public class homecontroller: controller {     // .... } 

update: additional flexibility of filter approach, if need filter on per-action basis, instead of actions in controller, it's possible:

public class homecontroller: controller {     [myactionfilter]     public actionresult myaction()     {         //...     } } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -