简体   繁体   English

在 asp.net core 上使用来自不同项目的另一个控制器

[英]Use another controller from different project on asp.net core

We trying to rewrite out old project to form old mvc to the asp.net core mvc.我们试图重写旧项目以形成旧的 mvc 到 asp.net 核心 mvc。 we used to attach another mvc project as reference and called the controller without problem.我们曾经附加另一个 mvc 项目作为参考,并且毫无问题地调用了控制器。 but on the new asp.net core MVC it seems it need another work around但是在新的 asp.net 核心 MVC 上,它似乎需要另一种解决方法

在此处输入图片说明

so the webapplication9 added webapplication1 as reference.所以 webapplication9 添加了 webapplication1 作为参考。 and call the canary but it return 404. it worked on old mvc.并调用金丝雀,但它返回 404。它在旧的 mvc 上工作。

the controller just return the plain content控制器只返回纯内容

namespace WebApplication1.Controllers
{
    public class CanaryController : Controller
    {
        public IActionResult Index()
        {
            return Content("Canary");
        }
    }
}

is there some part need to add to make webapplication9 able to call the webapplication1 controller?是否需要添加某些部分才能使 webapplication9 能够调用 webapplication1 控制器?

on old MVC 5 we can just called it on URL http://webapplication9/Canary and it works.在旧的 MVC 5 上,我们可以在 URL http://webapplication9/Canary上调用它,它可以工作。 on asp.net core it return 404 or not found在asp.net核心上它返回404或未找到

First, your other projects shouldn't be an Web application, but a normal PCL.首先,您的其他项目不应该是 Web 应用程序,而是普通的 PCL。

Second, after you reference it, you need to tell ASP.NET Core that it should look for controllers there.其次,在你引用它之后,你需要告诉 ASP.NET Core 它应该在那里寻找控制器。 For that reason the Application Parts Api exists.出于这个原因, 应用程序部分Api 存在。

services.AddMvc()
    .AddApplicationPart(typeof(CanaryController).GetTypeInfo().Assembly);

PS while the docs state it should work out of the box, when doing integration tests/unit tests on the controllers the .AddApplicationPart is still required, at least was the case in ASP.NET Core 2.0 PS,虽然文档说明它应该开箱即用,但在控制器上进行集成测试/单元测试时,仍然需要.AddApplicationPart ,至少在 ASP.NET Core 2.0 中是这样

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发布时从另一个ASP.NET Core 2项目引用ASP.NET Core 2项目失败 - Referencing ASP.NET Core 2 project from another ASP.NET Core 2 project fails when publishing 如何在 ASP.NET MVC Core 中使用来自另一个项目的 ViewModel - How to use ViewModels from another project in ASP.NET MVC Core 如何在 ASP.NET Core 2.2 中使用来自不同托管项目的共享 SignalR Hub - How to use shared SignalR Hub from different hosted project in ASP.NET Core 2.2 从另一个项目初始化ASP.NET Core Web API - Initialize ASP.NET Core Web API from another project ASP.NET Core:从解决方案中的另一个项目访问appsettings - ASP.NET Core: Accessing appsettings from another project in solution 在 asp.net 内核中看不到来自另一个项目的 DbContext - Cannot see the DbContext from another project in asp.net core 如何从 ASP.NET Core 6 MVC EF 中的 controller 中的另一个 controller 实例化服务? - How do I instantiate a service from another controller in a controller in ASP.NET Core 6 MVC EF? 如何从 ASP.NET Core MVC 中的一个控制器调用或返回另一个控制器的视图 - How to call or return the view of another controller from one controller in ASP.NET Core MVC 调用另一个控制器,并对调用的控制器操作MVC ASP.NET使用不同的布局 - Call another controller and use a different Layout for the called controller action MVC ASP.NET 在ASP.NET核心中使用时,如何从另一个项目访问EF核心的DbContext? - How to I access the DbContext of EF core from another project when used in ASP.NET core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM