标签: razorlight

项目找不到带有键_Layout.cshtml的模板

我在使用_Layout.cshtmlin的嵌入式资源时正在努力使电子邮件正常工作FluentEmail

这是我收到的异常错误:

Project can not find template with key _Layout.cshtml
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止的设置:

在ConfigureServices中Program.cs,我添加了RazorRenderer以下内容:

//Set email service using FluentEmail
 services.AddFluentEmail("appname@domain.com")
 .AddRazorRenderer(typeof(Program))
 .AddSmtpSender("smtp.somesmtp.com", 25)
 .AddSmtpSender(new System.Net.Mail.SmtpClient() { });
Run Code Online (Sandbox Code Playgroud)

在我的内部NotificationService.cs,我总是陷入异常块:

private async Task SendEmailAsync<TModel>(string subject, TModel model)
{
    try
    {
        using (var scope = _serviceProvider.CreateScope())
        {
            var email = await scope.ServiceProvider.GetRequiredService<IFluentEmail>()
                    .To(string.Join(";", _emailRecipients))
                    .Subject(subject)
                    .UsingTemplateFromEmbedded("AppName.Views.Emails.SomeReport.cshtml", model, GetType().Assembly)
                    .SendAsync();
        }
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to send email. Check exception for more information."); …
Run Code Online (Sandbox Code Playgroud)

c# razor .net-5 razorlight

7
推荐指数
1
解决办法
1440
查看次数

RazorLight RazorLight.TemplateNotFoundException:项目找不到带有密钥的模板

我正在尝试基于嵌入的 .cshtml 文件生成 HTML:

<EmbeddedResource Include="Templates\ReportTemplate.cshtml" />
Run Code Online (Sandbox Code Playgroud)

然而,从 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)
Run Code Online (Sandbox Code Playgroud)

但是当我检查嵌入资源时,我可以清楚地看到嵌入资源:

string[] files = Assembly.GetExecutingAssembly().GetManifestResourceNames();
Run Code Online (Sandbox Code Playgroud)

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 …
Run Code Online (Sandbox Code Playgroud)

.net razor razorengine asp.net-core razorlight

6
推荐指数
0
解决办法
2783
查看次数

标签 统计

razor ×2

razorlight ×2

.net ×1

.net-5 ×1

asp.net-core ×1

c# ×1

razorengine ×1