[英]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个项目
HR Project
HR项目
ACC Project
ACC项目
I want to compile into dll
我想编译成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模块。
If you want to do this you'll need to do the following 2 steps: 如果你想这样做,你需要做以下两个步骤:
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 theStartup
class you have to call the following:在
Startup
类的ConfigureServices
方法中,您必须调用以下内容:services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();
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. 而且要明确,我同意其他评论,我认为它不是一个好的架构,但如果你愿意,它可以做到。
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:
在这种情况下,您需要一个更像是的结构:
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应用程序,那么这些应用程序非常简单:大多数只包含
Program
和Startup
和依赖于各自的RCL。
For more information, see the documentation on Razor Class Libraries . 有关更多信息,请参阅Razor类库的文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.