当使用带有ASP.net核心2.1的Razor类库时,如果我添加对类库的引用,它会按预期加载控制器和视图 . 但问题是,如何在运行时动态加载这些模块?我想将模块放在目录中,这些模块在设计时未被引用,并在应用程序启动时加载它们 . 我试图使用应用程序部件 . 但是这样,加载了控制器,但是没有发现视图 .
我完全忘记了CompiledRazorAssemblyPart .
我们需要做的是:
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) { } } }
1 回答
我完全忘记了CompiledRazorAssemblyPart .
我们需要做的是:
并配置这样的部分