简体   繁体   中英

need to call controller's action method into another class in c# mvc

I have created one method in a controller, but I need to call this method into anther class and I don't know how to call it.

my sample code :-

  public class ReportController : BaseController
    {
        private readonly IPermissionService _permissionService;
        private readonly ICompaniesService _companyService;

       public ReportController(IPermissionService permissionService,
            ICompaniesService companyService)
       {
            this._permissionService = permissionService;
            this._companyService = companyService;
       }

        public void Reporting()
        {
            // code
        }
      }


    public class Home {

        public void Execute()
        {
            //I need to execute the Reporting method here
        }

    }

I have tried many things to call my method in another class method but I can't get it to work.

It's a bad design way to have some code in a controller who have to be called in several places. Create a class in your project or in an external DLL who contains this code and call this class in the controllers who need this method.

Create a class somewhere (in you project, or in a class library) :

public class MyClass
{
    public MyClass()
    {}

    public void MyMethod()
    {
      // Some code here
    }
}

And implement this code in your controllers or classes who need this.

public class ReportController : BaseController
    {
        private readonly IPermissionService _permissionService;
        private readonly ICompaniesService _companyService;

       public ReportController(IPermissionService permissionService,
            ICompaniesService companyService)
       {
            this._permissionService = permissionService;
            this._companyService = companyService;
       }

        public void Reporting()
        {
            MyClass myClass = new MyClass();
            myClass.MyMethod();
        }
}

By the way, if your code doesn't need any instance, you can create a static class, like :

public static class MyClass
{
    public static void MyMethod()
    {
       // Some code here
    }
}

public class ReportController : BaseController
{
       private readonly IPermissionService _permissionService;
       private readonly ICompaniesService _companyService;

       public ReportController(IPermissionService permissionService,
            ICompaniesService companyService)
       {
            this._permissionService = permissionService;
            this._companyService = companyService;
       }

        public void Reporting()
        {
            MyClass.MyMethod();
        }
}

There are two ways of accomplishing this. You are using dependency injection in your controller's constructor, so you have two options of doing this (not sure if I have missed any?):

  • Create an instance of each of your services in the calling method and pass these instances through when instantiating the controller, or
  • Add a constructor to your home class, set the services and pass them through when instantiating the controller

Option 1:

public class Home
{
     public void Execute()
     {
          // Create the variables that are needed by the constructor
          IPermissionService permissionService = new PermissionService();
          ICompaniesService companiesService = new CompaniesService();

          // Create an instance of your controller and pass through
          // the variables created above
          ReportController reportController = new ReportController(permissionService, companiesService);

          // Now that you have an instance of your controller call the method
          reportController.Reporting();
     }
}

It works on the same principle as creating an instance of a class and then calling its methods.

Option 2:

public class Home
{
     private IPermissionService permissionService;
     private ICompaniesService companiesService;

     public Home(IPermissionService permissionService, ICompaniesService companiesService)
     {
          this.permissionService = permissionService;
          this.companiesService = companiesService;
     }

     public void Execute()
     {
          ReportController reportController = new ReportController(permissionService, companiesService);

          reportController.Reporting();
     }
}
ReportController reportController = new ReportController();
reportController.Reporting();

As you would any other class that's method isn't static

Please read this answer, they go into a lot more detail than I do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM