简体   繁体   English

需要在c#mvc中将控制器的action方法调用到另一个类中

[英]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. 在您的项目或包含此代码的外部DLL中创建一个类,并在需要此方法的控制器中调用该类。

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: 选项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: 选项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 请阅读答案,它们比我更详细

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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