.net - RazorLight RazorLight.TemplateNotFoundException:项目找不到带有密钥的模板
问题描述
我正在尝试基于嵌入的 .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
解决方案
推荐阅读
- elasticsearch - elasticsearch中不同字段的两个过滤器(RANGE)
- r - 闪亮上传数据并探索崩溃
- python-3.x - 无法关闭 pandas.concat 中的“排序”功能
- python - 使用 pandas 快速生成部分 DataFrame
- tensorflow - Keras 在前 2 个 epoch 使用 GPU,然后停止使用它
- authentication - 如何通过 TLS 使用 flink-pulsar 连接器身份验证来使用 pulsar?
- c# - 调用 C# Windows 运行时组件时 C++/WinRT 项目崩溃
- python - 如何使用烧瓶错误解决 SQL Server 连接问题(不支持 Windows 身份验证)
- c++ - 在无序字符串集中查找字符串的时间复杂度
- python - Cloud Function 发生未知错误:GCP Python