【问题标题】:Is it possible to call normal controller Method/ActionResult from Web API Controller's Method?是否可以从 Web API 控制器的方法中调用普通控制器的 Method/ActionResult?
【发布时间】:2018-01-24 06:36:28
【问题描述】:

是否可以从Web API的方法调用普通MVC控制器的ActionResult/Method?

public class MyAPIController : ApiController
{
    public string CallActionResultOfMVCController()
    {
         //Here I want to call method/ActionResult from MyMVCController something like below.
         //var result = WantToCallMyMVCMethod();
         //return result;
    }
}

public class MyMVCController : Controller
{
    public string WantToCallMyMVCMethod()
    {
        return "test";
    }
}

提前致谢。

【问题讨论】:

  • 你为什么要这样做!我认为你的场景有问题。通常,如果我们要调用 MVC 控制器,它可能是从页面调用。但是您可以从页面或另一个 MVC 控制器等调用 API 方法...最后,如果您想在在线页面上搜索,您将看到“如何从 mvc 控制器调用 api 控制器方法”
  • 这似乎是一个 XY 问题。应该没有理由这样做。如果有通用代码,它不应该在控制器方法中。请说明你想通过这个实现什么。
  • @AminSaadati :是的,我确实从普通控制器调用 API 控制器方法,这没有问题。但是有一种情况我需要这种相反的情况。

标签: c# asp.net-mvc asp.net-web-api asp.net-web-api2 cross-platform


【解决方案1】:

虽然,我在上面的评论中告诉过你(可能是你的场景有问题),但无论如何你都想做。您可以使用以下链接进行操作。 成功的。 C# how to call MVC Action method from console application

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    相关资源
    最近更新 更多