[英]asp.net core mvc: split hosting and business-logic/ui into separate projects
I want to separate my solution into at least two parts: 我想将我的解决方案分成至少两部分:
because I want the hosting configuration to be replacable in later stages of the development process. 因为我希望托管配置在开发过程的后期阶段可以替换。
I tried simply moving all the folders containing controllers, models and views into a separate project, like shown in the image below: 我尝试将包含控制器,模型和视图的所有文件夹移动到一个单独的项目中,如下图所示:
Two projects with hosting configuration and business-logic separated: 托管配置和业务逻辑分离的两个项目:
So I 所以我
using Microsoft.AspNetCore.Mvc;
namespace MyApp.Implementation.Controllers
{
public class ExampleController : Controller
{
public ActionResult<int> Index()
{
return 5;
}
}
}
If I start the application and open http://localhost:5000/example in my browser, I get the result "5" in my browser. 如果我启动应用程序并在浏览器中打开http:// localhost:5000 / example ,我会在浏览器中得到结果“5”。 This shows to me that the hosting technology finds my controller in the separate project. 这告诉我托管技术在单独的项目中找到我的控制器。
But when I open http://localhost:5000 in the browser, I get an exception page telling me that the views for the Home-Controller where not found. 但是当我在浏览器中打开http:// localhost:5000时,我得到一个异常页面,告诉我没有找到Home-Controller的视图。 The Console also shows the exception: 控制台还显示异常:
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml
Since the webhost finds my controller, I would expect that it finds the views too. 由于webhost找到了我的控制器,我希望它也能找到它。 It seems not so. 似乎不是这样。
How can I tell the webhost where to look for the views instead? 如何告诉webhost在哪里查找视图? Or do I need to do anything to them instead? 或者我需要对他们做任何事情吗?
In addition to Kirk Larkin's comment to look at Application Parts in ASP.NET Core , you might also want to check out Razor Class Libraries . 除了Kirk Larkin关于在ASP.NET Core中查看应用程序部分的评论之外,您可能还想查看Razor类库 。
I have not tried it myself yet, but it looks it might provide a solution for your issue. 我还没有尝试过,但看起来它可能会为您的问题提供解决方案。
The problem when you move your controller folder, it can not detect your controllers anymore in your Startup.cs
. 移动控制器文件夹时出现问题,它无法在Startup.cs
检测到控制器。
There should be a line in there saying: 那里应该有一条线说:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
According to this link, what you should do is add a namespace to it like so: 根据这个链接,你应该做的是添加一个名称空间,如下所示:
app.UseMvc(routes =>
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Foo", action = "Index", id = UrlParameter.Optional },
// This will prioritize routes within your main application
namespaces: new[] { "ProjectA.Controllers"}
);
});
Hope this will be useful for you. 希望这对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.