更新时间:2022-03-08 21:04:51
如果要执行此操作,则需要执行以下2个步骤:
If you want to do this you'll need to do the following 2 steps:
There is already a solution for this on ***: How to use a controller in another assembly in ASP.NET Core MVC 2.0?
它表示以下内容:
在
Startup
类的ConfigureServices
方法内部调用以下内容:
Inside the
ConfigureServices
method of theStartup
class you have to call the following:
services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();
在***上也已经有解决方案:
There is also already a solution for this on ***:
应如何使用CompiledRazorAssemblyPart加载Razor视图?/a>
How CompiledRazorAssemblyPart should be used to load Razor Views?
这是上面链接中的一种可能的解决方案:
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) { }
}
}