Web API访问控制器方法来自另一个控制器方法 [英] Web api access controller method from another controller method

查看:93
本文介绍了Web API访问控制器方法来自另一个控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的控制器->控制器A,控制器B

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

每个控制器及其返回值都有不同的方法 IHttpActionResult(方法A控制器A,方法B和控制器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

控制器B和内部方法B

Controller B ,and Inside Method B

IHttpActionResult result = ControllerA.MethodA()

我想读取控制器B中的result.content

and I want to read result.content inside controller B

推荐答案

我会考虑为两个控制器使用一个通用的基类(如果要在两个控制器上都使用一种方法)

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

例如

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

然后像使用它们

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

        // Do something else
    }
}

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

        // Do Something else
    }
}

这篇关于Web API访问控制器方法来自另一个控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆