简体   繁体   English

asp.net core mvc:将托管和业务逻辑/ ui拆分为单独的项目

[英]asp.net core mvc: split hosting and business-logic/ui into separate projects

I want to separate my solution into at least two parts: 我想将我的解决方案分成至少两部分:

  • Hosting-technology (Initializing Kestrel and setting up all the middleware, eg swashbuckle, authentication) 托管技术(初始化红隼和设置所有中间件,例如swashbuckle,身份验证)
  • Business-Logic & UI 业务逻辑和用户界面

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 所以我

  • moved those folders to the *.Implementation project 将这些文件夹移动到* .Implementation项目
  • added a nuget-reference to the package "Microsoft.AspNetCore.Mvc" 为“Microsoft.AspNetCore.Mvc”软件包添加了一个nuget-reference
  • referenced the *.Implementation project from the *.Host project 引用* .Host项目中的* .Implementation项目
  • added this class to the "Controllers"-folder in the *.Implementation project: 将此类添加到* .Implementation项目中的“Controllers”文件夹:
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.

相关问题 ASP.NET Core MVC 中的业务逻辑 - Business logic in ASP.NET Core MVC MVC 如何将业务逻辑与 ASP.NET MVC 中的 UI 分开? - How MVC separate business logic from UI in ASP.NET MVC? 我需要分开业务对象和业务逻辑吗? C#中具有存储库模式的Asp.net MVC - Do I need to Separate Business Objects and Business Logic? Asp.net MVC with Repository Pattern in C# 如何将ASP.NET MVC核心视图和ASP.NET WEB API控制器分离到单独的项目中? - How to seperate ASP.NET MVC core view and ASP.NET WEB API controllers into separate projects? 将ASP.Net Core 2(.NET Framework)Web Api拆分为类库和托管项目是否可行/明智? - Is it possible/sensible to split ASP.Net Core 2 (.NET Framework) Web Api into Class Library and Hosting projects? ASP.NET CORE 将业务逻辑添加到模型而不是控制器 - ASP.NET CORE Adding Business Logic to Model instead of Controller 减少/删除 ASP.NET 核心中的重复业务逻辑检查 - Reduce/remove repetitive business logic checks in ASP.NET Core ASP.NET MVC:这个业务逻辑应该在哪里? - ASP.NET MVC: Where should this business logic go? ASP.NET MVC自定义验证业务逻辑 - ASP.NET MVC Custom Validation business logic 在一个ASP.NET MVC 5解决方案中具有单独的项目 - Having separate projects in one ASP.NET MVC 5 solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM