简体   繁体   English

如何将asp.net核心mvc项目分成多个程序集(.dll)?

[英]How to separate asp.net core mvc project into multiple assembly (.dll)?

How to separate asp.net core mvc project into multiple assembly (.dll)? 如何将asp.net核心mvc项目分成多个程序集(.dll)?

I have 3 projects 我有3个项目

  • MyApp Project MyApp项目
    • Controllers 控制器
      • HomeController.cs HomeController.cs
    • Models 楷模
    • Views 查看
      • Home
        • Index.cshtml Index.cshtml
  • HR Project HR项目

    • Controllers 控制器
      • EmployeeController.cs EmployeeController.cs
    • Models 楷模
      • EMPLOYEE.cs EMPLOYEE.cs
    • Views 查看
      • Employee 雇员
        • Index.cshtml Index.cshtml
  • ACC Project ACC项目

    • Controllers 控制器
      • ChartAccountController.cs ChartAccountController.cs
    • Models 楷模
      • ACCOUNT.cs ACCOUNT.cs
    • Views 查看
      • ChartAccount ChartAccount
        • Index.cshtml Index.cshtml

I want to compile into dll 我想编译成dll

  • HR Project HR项目
    • HR.dll HR.dll
    • HR.Views.dll HR.Views.dll
  • ACC Project ACC项目
    • ACC.dll ACC.dll
    • ACC.Views.dll ACC.Views.dll

I want to add reference those dll (HR.dll, HR.Views.dll, ACC.dll, ACC.Views.dll) into MyApp Project. 我想将这些dll(HR.dll,HR.Views.dll,ACC.dll,ACC.Views.dll)的引用添加到MyApp Project中。

And then run MyApp project can access Employee & Chart Account module too. 然后运行MyApp项目也可以访问Employee&Chart Account模块。

*** Original Answer (Update below) ***原始答案(下面更新)

If you want to do this you'll need to do the following 2 steps: 如果你想这样做,你需要做以下两个步骤:

  1. You need to load the controllers from the external dlls 您需要从外部dll加载控制器

There is already a solution for this on stackoverflow: How to use a controller in another assembly in ASP.NET Core MVC 2.0? stackoverflow上已有解决方案: 如何在ASP.NET Core MVC 2.0中的另一个程序集中使用控制器?

It says the following: 它说如下:

Inside the ConfigureServices method of the Startup class you have to call the following: Startup类的ConfigureServices方法中,您必须调用以下内容:

 services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices(); 
  1. You need to load the your compiled Razor views, I guess this is what you have in your HR.Views.dll and ACC.Views.dll. 您需要加载已编译的Razor视图,我想这就是您在HR.Views.dll和ACC.Views.dll中所拥有的。

There is also already a solution for this on stackoverflow: stackoverflow上还有一个解决方案:

How CompiledRazorAssemblyPart should be used to load Razor Views? 如何使用CompiledRazorAssemblyPart加载Razor视图?

Loading Razor Class Libraries as plugins 将Razor类库加载为插件

This is one possible solution from the links above: 这是上面链接中的一种可能的解决方案:

What you need to do is: 你需要做的是:

 services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .ConfigureApplicationPartManager(ConfigureApplicationParts); 

and configure the parts like this 并配置这样的部分

  private void ConfigureApplicationParts(ApplicationPartManager apm) { var rootPath = HostingEnvironment.ContentRootPath; var pluginsPath = Path.Combine(rootPath, "Plugins"); var assemblyFiles = Directory.GetFiles(pluginsPath, "*.dll", SearchOption.AllDirectories); foreach (var assemblyFile in assemblyFiles) { try { var assembly = Assembly.LoadFile(assemblyFile); if (assemblyFile.EndsWith(".Views.dll")) apm.ApplicationParts.Add(new CompiledRazorAssemblyPart(assembly)); else apm.ApplicationParts.Add(new AssemblyPart(assembly)); } catch (Exception e) { } } } 

If you also have javascript and css files in our separate MVC projects, you will need to embed this to your dll otherwise your main app wont see it. 如果你在我们单独的MVC项目中也有javascript和css文件,你需要将它嵌入你的dll,否则你的主应用程序将看不到它。 So in your HR and ACC project, you'll need to add this in your .csproj file: 因此,在您的HR和ACC项目中,您需要在.csproj文件中添加它:

  <ItemGroup>
    <EmbeddedResource Include="wwwroot\**" />
  </ItemGroup>

And just to be clear, I agree with the other comments, I don't think it is a good architecture, but it is possible to do it if you want. 而且要明确,我同意其他评论,我认为它不是一个好的架构,但如果你愿意,它可以做到。

*** Updated (Worked) ***更新(工作)

Just a step: 只需一步:

Edit Startup.cs in ConfigureServices method ConfigureServices方法中编辑Startup.cs

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).ConfigureApplicationPartManager(ConfigureApplicationParts);

and method: 和方法:

private void ConfigureApplicationParts(ApplicationPartManager apm)
    {
        var rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

        var assemblyFiles = Directory.GetFiles(rootPath , "*.dll");
        foreach (var assemblyFile in assemblyFiles)
        {
            try
            {
                var assembly = Assembly.LoadFile(assemblyFile);
                if (assemblyFile.EndsWith(this.GetType().Namespace + ".Views.dll") || assemblyFile.EndsWith(this.GetType().Namespace + ".dll"))
                    continue;
                else if (assemblyFile.EndsWith(".Views.dll"))
                    apm.ApplicationParts.Add(new CompiledRazorAssemblyPart(assembly));
                else
                    apm.ApplicationParts.Add(new AssemblyPart(assembly));
            }
            catch (Exception e) { }
        }
    }

What you're wanting is not possible, or perhaps better said: it's not a good idea, for sure. 你想要的是不可能的,或者更好的说法:肯定不是一个好主意。

In general, you need to use Razor Class Libraries. 通常,您需要使用Razor类库。 All common functionality would then go into those RCLs. 然后,所有常见功能将进入那些RCL。 Your entire HR and ACC projects could be RCLs, unless you need to run those independently, as full web apps, as well. 您的整个HR和ACC项目可能是RCL,除非您需要独立运行这些项目,作为完整的Web应用程序。 In which case, you'd need a structure more like: 在这种情况下,您需要一个更像是的结构:

  • HR RCL HR RCL
  • HR Web App (depends on HR RCL) HR Web App(取决于HR RCL)
  • ACC RCL ACC RCL
  • ACC Web App (depends on ACC RCL) ACC Web App(取决于ACC RCL)
  • Main App (depends on HR RCL and ACC RCL) 主要应用程序(取决于HR RCL和ACC RCL)

In either case, you'd put all the controllers, views, and static resources that need to be shared in the RCL, so if you do have actual HR/ACC web apps, those would be pretty light: mostly just consisting of Program and Startup and a dependency on their respective RCLs. 在任何一种情况下,您都需要在RCL中共享所有需要共享的控制器,视图和静态资源,因此,如果您确实拥有实际的HR / ACC Web应用程序,那么这些应用程序非常简单:大多数只包含ProgramStartup和依赖于各自的RCL。

For more information, see the documentation on Razor Class Libraries . 有关更多信息,请参阅Razor类库文档

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

相关问题 ASP.NET Core MVC主项目无法在单独的程序集中到达Controllers - ASP.NET Core MVC main project can't reach Controllers in separate assembly 加载 ASP.NET Core MVC 5 项目的程序集时出现 FileNotFoundException - FileNotFoundException when loading assembly of ASP.NET Core MVC 5 project 在单独的项目Asp.net Core MVC中进行本地化 - Localization in separate project Asp.net Core MVC ASP.NET MVC与模型在单独的程序集中 - ASP.NET MVC with Model in Separate Assembly 如何从 ASP.NET Core MVC 项目的 dll 获取类型 - How to get types from dll of an ASP.NET Core MVC project 如何在ASP.NET Core项目中包含对程序集的引用 - How to include reference to assembly in ASP.NET Core project 如何访问ASP.NET MVC项目的bin文件夹中的DLL? - How to access DLL in bin folder of ASP.NET MVC project? ASP.NET Core Web Application(.NET Framework)项目模板缺少System.Web.MVC dll - ASP.NET Core Web Application (.NET Framework) project template is missing System.Web.MVC dll 如何将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核心mvc项目中的jquery错误? - how to fix jquery error in asp.net core mvc project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM