从MVC控制器消耗Web API控制器动作 [英] Consuming web api controller action from mvc controller

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

问题描述

我有两个控制器,一个mvc控制器和一个api控制器,它们都在同一个项目中.

I have two controllers, one mvc controller and one api controller, both are in the same project.

HomeController: Controller{ ... }
DataController: ApiController{ ... }

如果我想从HomeController中使用DataController中的发布操作,是否有必要使用HttpClient?

If I want to consume post action in DataController from HomeController is it necessary to use HttpClient?

推荐答案

不是必需的.您可以直接在HomeController中创建DataController对象的对象,并使用它的post方法(如.Net中的实例方法).

Not required. You can directly create the object of DataController object in HomeController and use its post method like instance method in .Net.

public class HomeController : Controller
    {

        public ActionResult GetResult()
        {
            MyApp.DataController dataController = new MyApp.DataController();
            var data = dataController.Post("arguments");

            return View(data);
        }
    }

这篇关于从MVC控制器消耗Web API控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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