[英]Loading TagHelpers from an external assembly
在带有剃刀视图的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();
}
因此,当使用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));
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.