零基础ASP.NET Core MVC插件式开发
//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMvc().ConfigureApplicationPartManager(apm =>
{
// 和主项目里面注册插件项目类似,这里注册主程序dll,主要是为了在vs里面直接运行调试,不需要发布到主项目用主项目来运行
// 这样方便插件的开发与调试
// 匹配查找运行目录下的主项目插件文件
var targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
var plugAss = Directory.EnumerateFiles(targetPath, "Agile.WebPlatform.*.dll").Select(AssemblyLoadContext.Default.LoadFromAssemblyPath);
//加载主程序 Assemlby 并注册到 MvcBuilder 中
foreach (var item in plugAss)
{
foreach (var part in new DefaultApplicationPartFactory().GetApplicationParts(item))
{
apm.ApplicationParts.Add(part);
}
foreach (var part in new CompiledRazorAssemblyApplicationPartFactory().GetApplicationParts(item))
{
apm.ApplicationParts.Add(part);
}
}
});