asp.net mvc - .Net MVC When to use OnActionExecuting? -


some basic question.

when initialize or assign in onactionexecuting?

very simple scenario:

public partial class ordercontroller : defaultcontroller {     private int customerid = 0;      protected override void onactionexecuting(actionexecutingcontext filtercontext)     {         customerid = websecurity.currentuserid;          base.onactionexecuting(filtercontext);     } } 

should assign customerid in onactionexecuting can reuse in code in each action?

  • readability
  • eliminating repeating

or should assign in separate actions each time?

  • we not need assignment or in case of initialization of object in each action)?

the answer is, of course, it depends on team's coding style , other circumstances.

another option haven't considered creating custom action filter contain logic, , reuse 1 line of code above action-method signature.

then if decide apply all/any action-methods, can apply controller, or base-controller even.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -