我正在使用 ASP.NET 核心 2.1。我从插件文件夹中动态加载所有具有视图的程序集。我为此使用以下代码。视图被正确加载。
services.AddMvc().
AddRazorPagesOptions(o => o.AllowAreas = true).
SetCompatibilityVersion(CompatibilityVersion.Version_2_1).
ConfigureApplicationPartManager(ConfigureApplicationParts);
private void ConfigureApplicationParts(ApplicationPartManager apm)
{
var pluginsPath = Path.Combine(_env.WebRootPath, "Plugins");
var assemblyFiles = Directory.GetFiles(pluginsPath, "*.dll", SearchOption.AllDirectories);
foreach (var assemblyFile in assemblyFiles)
{
var assembly = Assembly.LoadFile(assemblyFile);
if (assemblyFile.EndsWith(".Views.dll"))
{
apm.ApplicationParts.Add(new CompiledRazorAssemblyPart(assembly));
}
else
{
apm.ApplicationParts.Add(new AssemblyPart(assembly));
}
}
}
这些视图有一些自定义标记助手。
_ViewImports.cshtml文件看起来像
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, MyTagHelpers
问题是自定义标签助手没有被加载并给出错误:
错误:无法加载文件或程序集 MyTagHelpers
我收到错误的原因可能是 Razor View Engine 可能正在主应用程序的bin文件夹中查找 DLL,但它找不到 DLL 并给出此错误。
在启动时我应该怎么做才能说 taghelper 在 DLL 中可用并且可以从那里加载?我应该使用TagHelperFeatureProvider吗?
更新:我将标签助手移动到一个名为MyTagHelpers.Common的单独 DLL中,并放入插件文件夹中。我不再收到任何未找到任何程序集的错误,但标签助手不起作用。