2

I am having two controller. 1st controller having parameterized constructor and some methods. Now I have to call that methods in my another controller. is there any way to do it?

Below is code

public partial class oneController : Controller
{
   private readonly IEmployeeService _employeeService;
   public oneController(IEmployeeService employeeService)
   {
      this._employeeService = employeeService;
   }

   // some methods


}

public partial class twoController : Controller
{
  // Need to call some methods from oneController
}
5
  • Both are in same file or separate files? Commented Aug 29, 2016 at 9:21
  • Both controller having separate files.. Commented Aug 29, 2016 at 9:22
  • return RedirectToAction("youactionname","twoController", new{ your parameters }) .. Commented Aug 29, 2016 at 9:25
  • Dear mmushtaq, I don't redirect.. I just want to call method with param and get return value in 2nd controller action.. Means I don't want to repeat method that I already created in 1st controller. Commented Aug 29, 2016 at 9:29
  • stackoverflow.com/questions/16870413/… this can be your solution Commented Aug 29, 2016 at 9:34

1 Answer 1

0

You can achieve this as follow:

public partial class twoController : Controller{
     oneController  one = new oneController();
     one.AnyMethod(AnyParam);
}

But you're trying to do something the controllers aren't designed for. if you have some common method which is being accessible from multiple controllers then create required method as an public method in some class and invoke from any controller/actions you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.