[英]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.