7

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

4 Answers 4

8

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 (e.g. 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()
   }
}
Sign up to request clarification or add additional context in comments.

Comments

5

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
    }
}

Comments

3

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();
        }
    }

Comments

1
AController aController = new AController();
var getResponse = aController.YourMethod(values);

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

JavaScriptSerializer().Deserialize

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.