从外部程序集

时间:2018-01-30 12:50:40

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

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

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

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

如果从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();
  }

1 个答案:

答案 0 :(得分:0)

因此,当使用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));
    });    
  }