1

我正在使用 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中,并放入插件文件夹中。我不再收到任何未找到任何程序集的错误,但标签助手不起作用。

4

1 回答 1

2

在尝试解决此问题 2 天后 - 请注意 - “程序集名称”是已编译(组装?)的 .DLL 名称,它通常与可能与命名空间名称/前缀不匹配的项目名称匹配!

因此,如果您的项目名称与我的名称空间不同,则 @addTagHelper 引用是用于创建已编译 .DLL 的项目名称 - 请参阅您的构建输出以进行检查。

因此,这通常也与您的 .csproj 文件的前缀相同,这就是官方文档说要创建新应用程序的原因。

于 2018-09-02T09:26:21.123 回答