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

查看:167
本文介绍了如何将asp.net核心mvc项目分离为多个程序集(.dll)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有3个项目

  • MyApp项目
    • 控制器
      • HomeController.cs
      • 首页
        • Index.cshtml

        人力资源项目

        • 控制器
          • EmployeeController.cs
          • EMPLOYEE.cs
          • 员工
            • Index.cshtml

            ACC项目

            • 控制器
              • ChartAccountController.cs
              • ACCOUNT.cs
              • ChartAccount
                • Index.cshtml

                我想编译成dll

                • 人力资源项目
                  • HR.dll
                  • HR.Views.dll
                  • ACC.dll
                  • ACC.Views.dll

                  我想将引用这些dll(HR.dll,HR.Views.dll,ACC.dll,ACC.Views.dll)添加到MyApp Project中.

                  I want to add reference those dll (HR.dll, HR.Views.dll, ACC.dll, ACC.Views.dll) into MyApp Project.

                  然后运行MyApp项目就可以访问Employee&图表帐户模块.

                  And then run MyApp project can access Employee & Chart Account module too.

                  推荐答案

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

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

                  *** Original Answer (Update below)

                  If you want to do this you'll need to do the following 2 steps:

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

                  在stackoverflow上已经有解决方案:

                  There is already a solution for this on stackoverflow: How to use a controller in another assembly in ASP.NET Core MVC 2.0?

                  它表示以下内容:

                  Startup 类的 ConfigureServices 方法内部调用以下内容:

                  Inside the ConfigureServices method of the Startup class you have to call the following:

                  services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();
                  

                  1. 您需要加载编译后的Razor视图,我想这就是您的HR.Views.dll和ACC.Views.dll中的内容.

                  在stackoverflow上也已经有解决方案:

                  There is also already a solution for this on stackoverflow:

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

                  How CompiledRazorAssemblyPart should be used to load Razor Views?

                  将Razor类库作为插件加载

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

                  This is one possible solution from the links above:

                  您需要做的是:

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

                  并配置类似的部分

                      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) { }
                          }
                      }
                  

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

                  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. So in your HR and ACC project, you'll need to add this in your .csproj file:

                    <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.

                  只需一步:

                  使用 ConfigureServices 方法

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

                  和方法:

                  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) { }
                          }
                      }
                  

                  这篇关于如何将asp.net核心mvc项目分离为多个程序集(.dll)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆