简体   繁体   中英

Web api access controller method from another controller method

I have two different controller --> Controller A,Controller B

And I have different methods each controller and their return values IHttpActionResult (Method A controller A ,Method B and Controller B)

How can I access another controller method and take its content from another controller

Controller B ,and Inside Method B

IHttpActionResult result = ControllerA.MethodA()

and I want to read result.content inside controller B

When request comes, only controller which should process request is instantiated automatically. You can instantiate second controller manually, but I would recommend to move MethodA functionality either to base controller class

public class BaseController : ApiController
{
   // ...

   public IHttpActionResult MethodA(int id)
   {
       var foo = repository.Get(id);
       if (foo == null)
           return NotFound();

       return Ok(foo);
   }
}

public class ControllerA : BaseController
{
   //...
}

public class ControllerB : BaseController
{
   public IHttpActionResult MethodB(int id)
   {
       var result = MethodA();
       //..
   }
}

or move common logic to separate class (eg service), so you would be able to call it from both controllers.

public class ControllerA : ApiController
{
   private IFooService fooService;

   public ControllerA(IFooService fooService)
   {
       this.fooService = fooService;
   }

   public IHttpActionResult MethodA(int id)
   {
      // use fooService.Method()
   }
}

public class ControllerB : ApiController
{
   private IFooService fooService;

   public ControllerB(IFooService fooService)
   {
       this.fooService = fooService;
   }

   public IHttpActionResult MethodB(int id)
   {
        // use fooService.Method()
   }
}

I would consider using a common base class for the two controllers (if there is a method you want to use on both)

for example

public abstract class MyBaseController
{
    public void CommonMethod()
    {
        // Do something here
    }
}

then use them like

public class ControllerA : MyBaseController
{
    public void MethodA()
    {
        base.CommonMethod();

        // Do something else
    }
}

public class ControllerB : MyBaseController
{
    public void MethodB()
    {
        base.CommonMethod();

        // Do Something else
    }
}

1) You can use static class and static method inside to share it for another controllers

public static class CommonMethods
{
    public static string SomeMethod(string s)
    {
        string RetString;
        ...
        return (RetString);
    }
}

Now you can use it in any controllers

string SomeMethodResult = CommonMethods.SomeMethod("Say Hello");

2) And another method is to create an instance of a controller class and call instances methods:

public class V1Controller : ApiController
    {
        public void Put(int id, [FromBody]string value)
        {
            HomeController hc = new HomeController();
            hc.SomeMethod();
        }
    }
AController aController = new AController();
var getResponse = aController.YourMethod(values);

If your method returns Json then you can easily solve it with

JavaScriptSerializer().Deserialize

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