Sop*_*irk 5 c# asp.net asp.net-mvc asp.net-core-mvc asp.net-core
如何将asp.net core mvc 项目分成多个程序集(.dll)?
我有3个项目
人力资源项目
ACC项目
我想编译成dll
我想将引用这些 dll(HR.dll、HR.Views.dll、ACC.dll、ACC.Views.dll)添加到 MyApp 项目中。
然后运行 MyApp 项目也可以访问 Employee & Chart Account 模块。
如果您想执行此操作,则需要执行以下 2 个步骤:
stackoverflow 上已经有一个解决方案:How to use a controller in another assembly in ASP.NET Core MVC 2.0?
它说如下:
在类
ConfigureServices的方法中Startup,您必须调用以下内容:Run Code Online (Sandbox Code Playgroud)services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();
stackoverflow 上也已经有一个解决方案:
应如何使用 CompiledRazorAssemblyPart 加载 Razor 视图?
这是上面链接中的一种可能的解决方案:
你需要做的是:
Run Code Online (Sandbox Code Playgroud)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) { } } }
如果您在我们单独的 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)
| 归档时间: |
|
| 查看次数: |
4842 次 |
| 最近记录: |