【问题标题】:Loading TagHelpers from an external assembly从外部程序集加载 TagHelper
【发布时间】:2018-01-30 12:50:39
【问题描述】:

在带有 razor 视图的 ASP.NET Core 2.0 项目中,我正在运行时加载一个包含 TagHelpers 的程序集。

当 .dll 位于项目的 bin 文件夹中或将 TagHelpers 项目作为依赖项添加到项目时,标签由 taghelpers 解析。

但是,当程序集在 bin 文件夹外加载时,即使程序集成功加载,TagHelpers 也不起作用。

从 bin 外的文件夹加载程序集时,如何让 TagHelpers 工作?

 public void ConfigureServices(IServiceCollection services)
 {

    var asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(@"D:\SomeTagHelpers\bin\Debug\netcoreapp2.0\SomeTagHelpers.dll");
    var part = new AssemblyPart(asm);
    var builder = services.AddMvc();
    builder.ConfigureApplicationPartManager(appPartManager => appPartManager.ApplicationParts.Add(part));
    builder.AddTagHelpersAsServices();
  }

【问题讨论】:

    标签: c# asp.net-core asp.net-core-tag-helpers


    【解决方案1】:

    因此,当使用 bin 文件夹之外的引用时,请使用 RazorViewEngineOptions 的 AdditionalCompilationReferences 将引用添加到编译中,以便发现和工作标记助手。此外,不需要使用 AddTagHelpersAsServices()。

      public void ConfigureServices(IServiceCollection services)
      {
        var asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(@"D:\SomeTagHelpers\bin\Debug\netcoreapp2.0\SomeTagHelpers.dll");
        var part = new AssemblyPart(asm);
        var builder = services.AddMvc();
        builder.ConfigureApplicationPartManager(appPartManager => appPartManager.ApplicationParts.Add(part));    
    
        builder.Services.Configure((RazorViewEngineOptions options) =>
        {
          options.AdditionalCompilationReferences.Add(MetadataReference.CreateFromFile(asm.Location));
        });    
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      相关资源
      最近更新 更多