asp.net mvc - need to call controller's action method into another class in c# mvc -


i have created 1 method in controller, need call method anther class , don't know how 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 execute reporting method here         }      } 

i have tried many things call method in class method can't work.

it's bad design way have code in controller have called in several places. create class in project or in external dll contains code , call class in controllers need method.

create class somewhere (in project, or in class library) :

public class myclass {     public myclass()     {}      public void mymethod()     {       // code here     } } 

and implement code in controllers or classes 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 way, if code doesn't need instance, can create static class, :

public static class myclass {     public static void mymethod()     {        // 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();         } } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -