在 dotnet core 2 中生成 Razor HTML 电子邮件
Generate Razor HTML emails in dotnet core 2
如何在 dotnetcore 中使用 Razor 生成电子邮件 (html),而不是从 MVC 应用程序(从控制台应用程序考虑)?
RazorEngine 在 .net 中表现出色 4.x,但在 dotnet 核心中不工作。
RazorEngineLight 在 dotnet 核心 1.x 中工作,但在 2.x.
中不工作
此 post 中提到了一些其他选项: 但其中 none 实际上在 .net core 2.0 中有效
两年后编辑:
万一有人来这里寻找答案...我 (OP) 已经不再完全依赖 Razor 使用模板等生成电子邮件。它非常脆弱且容易出错 - 令人头疼不已。这些天我更喜欢 Mandrill 或 Sendgrid - 使用模板。
您在 的评论中说
I am not able to get this to work. I get the error: Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.Razor.IRazorViewEngine' while attempting to activate 'Mvc.RenderViewToString.RazorViewToStringRenderer'.'
这通常表示所需服务未在服务集合中注册,因此提供者无法在需要时解析该服务。
那个答案没有提到额外的服务配置,只有
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IViewRender, ViewRender>();
}
因为它已经 运行 在 Asp.Net 核心环境中,这意味着在控制台应用程序中手动添加的服务已经在启动时完成。
请注意 中的这个片段,该片段链接到您评论的答案。
private static void ConfigureDefaultServices(IServiceCollection services) {
var applicationEnvironment = PlatformServices.Default.Application;
services.AddSingleton(applicationEnvironment);
var appDirectory = Directory.GetCurrentDirectory();
var environment = new HostingEnvironment
{
WebRootFileProvider = new PhysicalFileProvider(appDirectory),
ApplicationName = "RenderRazorToString"
};
services.AddSingleton<IHostingEnvironment>(environment);
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Clear();
options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
});
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton<DiagnosticSource>(diagnosticSource);
services.AddLogging();
services.AddMvc();
services.AddSingleton<RazorViewToStringRenderer>();
}
上面重要的部分是
services.AddMvc();
这会将相关的视图引擎依赖项添加到服务集合中
MvcServiceCollectionExtensions.cs
public static IMvcBuilder AddMvc(this IServiceCollection services) {
//...code removed for brevity
// Default framework order
builder.AddFormatterMappings();
builder.AddViews();
builder.AddRazorViewEngine();
builder.AddRazorPages();
builder.AddCacheTagHelper();
//...code removed for brevity
}
当前呈现的其他所有内容都很好,应该按预期工作。
你应该复习一下
并遵循类似的结构来让代码在您的方案中工作。从那里您可以开始进行自定义修改并监控它在哪里中断。
.Net Core 的模块化特性允许进行自定义,因为可以剥离不同的模块并在其他环境中使用。
如何在 dotnetcore 中使用 Razor 生成电子邮件 (html),而不是从 MVC 应用程序(从控制台应用程序考虑)?
RazorEngine 在 .net 中表现出色 4.x,但在 dotnet 核心中不工作。
RazorEngineLight 在 dotnet 核心 1.x 中工作,但在 2.x.
中不工作此 post 中提到了一些其他选项:
两年后编辑:
万一有人来这里寻找答案...我 (OP) 已经不再完全依赖 Razor 使用模板等生成电子邮件。它非常脆弱且容易出错 - 令人头疼不已。这些天我更喜欢 Mandrill 或 Sendgrid - 使用模板。
您在
I am not able to get this to work. I get the error: Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.Razor.IRazorViewEngine' while attempting to activate 'Mvc.RenderViewToString.RazorViewToStringRenderer'.'
这通常表示所需服务未在服务集合中注册,因此提供者无法在需要时解析该服务。
那个答案没有提到额外的服务配置,只有
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IViewRender, ViewRender>();
}
因为它已经 运行 在 Asp.Net 核心环境中,这意味着在控制台应用程序中手动添加的服务已经在启动时完成。
请注意
private static void ConfigureDefaultServices(IServiceCollection services) {
var applicationEnvironment = PlatformServices.Default.Application;
services.AddSingleton(applicationEnvironment);
var appDirectory = Directory.GetCurrentDirectory();
var environment = new HostingEnvironment
{
WebRootFileProvider = new PhysicalFileProvider(appDirectory),
ApplicationName = "RenderRazorToString"
};
services.AddSingleton<IHostingEnvironment>(environment);
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Clear();
options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
});
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton<DiagnosticSource>(diagnosticSource);
services.AddLogging();
services.AddMvc();
services.AddSingleton<RazorViewToStringRenderer>();
}
上面重要的部分是
services.AddMvc();
这会将相关的视图引擎依赖项添加到服务集合中
MvcServiceCollectionExtensions.cs
public static IMvcBuilder AddMvc(this IServiceCollection services) {
//...code removed for brevity
// Default framework order
builder.AddFormatterMappings();
builder.AddViews();
builder.AddRazorViewEngine();
builder.AddRazorPages();
builder.AddCacheTagHelper();
//...code removed for brevity
}
当前呈现的其他所有内容都很好,应该按预期工作。
你应该复习一下
并遵循类似的结构来让代码在您的方案中工作。从那里您可以开始进行自定义修改并监控它在哪里中断。
.Net Core 的模块化特性允许进行自定义,因为可以剥离不同的模块并在其他环境中使用。