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

Sop*_*irk 5 c# asp.net asp.net-mvc asp.net-core-mvc asp.net-core

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

我有3个项目

  • 我的应用项目
    • 控制器
      • 家庭控制器.cs
    • 楷模
    • 观看次数
        • 索引.cshtml
  • 人力资源项目

    • 控制器
      • 员工控制器.cs
    • 楷模
      • EMPLOYEE.cs
    • 观看次数
      • 员工
        • 索引.cshtml
  • ACC项目

    • 控制器
      • ChartAccountController.cs
    • 楷模
      • 帐户.cs
    • 观看次数
      • 图表帐户
        • 索引.cshtml

我想编译成dll

  • 人力资源项目
    • 人力资源管理器
    • 人力资源视图文件
  • ACC项目
    • ACC文件
    • ACC.Views.dll

我想将引用这些 dll(HR.dll、HR.Views.dll、ACC.dll、ACC.Views.dll)添加到 MyApp 项目中。

然后运行 ​​MyApp 项目也可以访问 Employee & Chart Account 模块。

huj*_*omi 3

*** 原始答案(更新如下)

如果您想执行此操作,则需要执行以下 2 个步骤:

  1. 您需要从外部 dll 加载控制器

stackoverflow 上已经有一个解决方案:How to use a controller in another assembly in ASP.NET Core MVC 2.0?

它说如下:

在类ConfigureServices的方法中Startup,您必须调用以下内容:

services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();
Run Code Online (Sandbox Code Playgroud)
  1. 您需要加载已编译的 Razor 视图,我想这就是您的 HR.Views.dll 和 ACC.Views.dll 中的内容。

stackoverflow 上也已经有一个解决方案:

应如何使用 CompiledRazorAssemblyPart 加载 Razor 视图?

将 Razor 类库作为插件加载

这是上面链接中的一种可能的解决方案:

你需要做的是:

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.ConfigureApplicationPartManager(ConfigureApplicationParts);
Run Code Online (Sandbox Code Playgroud)

并像这样配置部件

    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) { }
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果您在我们单独的 MVC 项目中还有 javascript 和 css 文件,则需要将其嵌入到您的 dll 中,否则您的主应用程序将看不到它。因此,在您的 HR 和 ACC 项目中,您需要将其添加到 .csproj 文件中:

  <ItemGroup>
    <EmbeddedResource Include="wwwroot\**" />
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

需要明确的是,我同意其他评论,我不认为这是一个好的架构,但如果你愿意的话是可以做到的。

*** 更新(已工作)

只需一步:

ConfigureServices方法中编辑Startup.cs

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).ConfigureApplicationPartManager(ConfigureApplicationParts);
Run Code Online (Sandbox Code Playgroud)

和方法:

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) { }
        }
    }
Run Code Online (Sandbox Code Playgroud)