我正在尝试基于嵌入的 .cshtml 文件生成 HTML:
<EmbeddedResource Include="Templates\ReportTemplate.cshtml" />
但是,从 RazorLight 我总是得到以下异常:
RazorLight.TemplateNotFoundException: Project can not find template with key My.Namespace.Application.Templates.ReportTemplate.cshtml
at RazorLight.Compilation.RazorTemplateCompiler.CreateRuntimeCompilationWorkItem(String templateKey)
at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
at RazorLight.Compilation.RazorTemplateCompiler.CompileAsync(String templateKey)
at RazorLight.EngineHandler.CompileTemplateAsync(String key)
at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag)
但是当我检查嵌入式资源时,我可以清楚地看到嵌入式资源:
string[] files = Assembly.GetExecutingAssembly().GetManifestResourceNames();
files
包含:My.Namespace.Application.Templates.ReportTemplate.cshtml
这是我生成 PDF 的代码:
private async Task<string> RenderRazorTemplate(string key, Type viewModelType, object viewModel)
{
string[] files = Assembly.GetExecutingAssembly().GetManifestResourceNames();
var engine = new RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(Assembly
.GetExecutingAssembly()) // Use the Executing Assembly as project that embeds the .cshtml templates
.SetOperatingAssembly(viewModelType.Assembly) // Without this, you'll get a Exception saying that the Assembly can't find the AntiForgery.dll
.UseMemoryCachingProvider()
.Build();
return await engine.CompileRenderAsync(key, viewModel);
}
我的参数是:键:My.Namespace.Application.Templates.ReportTemplate.cshtml
我正在使用RazorLight.NetCore3
版本3.0.2