1

I have a method that will need to be called by various controllers and various actions. Where is the correct place to put this method and how would i call it from other controllers. Cant seem to find anything by searching, so if someone could point me in the right direction i would appreciate it.

Thanks

3
  • This usually depends on the function and responsibilities, none of which are posted. Is it a static method? Dec 8, 2013 at 11:11
  • 3
    Sounds like you could use a base, Controller-derived class for all your controllers.
    – CodeCaster
    Dec 8, 2013 at 11:17
  • Maybe if this functionality is the same for all controllers you could use global filter. Dec 8, 2013 at 11:44

1 Answer 1

6

Sounds like a job for a base controller:

public class BaseController : Controller 
{    
   // Do your common stuff here...
}

Then inherit BaseController:

public class MyController : BaseController
{    
   // Your code goes here...
}
4
  • 1
    Why base controller? Not all controllers need access to this method. It no doubt gets the job done but I will like to learn the reasoning behind this approach.
    – Yogesh
    Dec 9, 2013 at 2:31
  • 1
    @Yogesh "Not all controllers need access to this method" - then don't let those controllers inherit from it.
    – CodeCaster
    Dec 9, 2013 at 9:32
  • @CodeCaster my point is shouldnt it be static methods instead of base controller
    – Yogesh
    Dec 9, 2013 at 10:46
  • The question is not concrete enough to give such advise, also, I try to use static as little as possible.
    – CodeCaster
    Dec 9, 2013 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.