gpt4 book ai didi

c# - RazorEngine 字符串布局和部分?

转载 作者:IT王子 更新时间:2023-10-29 04:43:42 26 4
gpt4 key购买 nike

我像这样使用 Razor 引擎:

public class EmailService : IService
{
private readonly ITemplateService templateService;

public EmailService(ITemplateService templateService)
{
if (templateService == null)
{
throw new ArgumentNullException("templateService");
}
this.templateService = templateService;
}

public string GetEmailTemplate(string templateName)
{
if (templateName == null)
{
throw new ArgumentNullException("templateName");
}
Assembly assembly = Assembly.GetAssembly(typeof(EmailTemplate));
Stream stream = assembly.GetManifestResourceStream(typeof(EmailTemplate), "{0}.cshtml".FormatWith(templateName));
string template = stream.ReadFully();
return template;
}

public string GetEmailBody(string templateName, object model = null)
{
if (templateName == null)
{
throw new ArgumentNullException("templateName");
}
string template = GetEmailTemplate(templateName);
string emailBody = templateService.Parse(template, model, null, null);
return emailBody;
}
}

我使用的模板服务是注入(inject)的,尽管它只是一个默认实现:

    internal ITemplateService InstanceDefaultTemplateService()
{
ITemplateServiceConfiguration configuration = new TemplateServiceConfiguration();
ITemplateService service = new TemplateService(configuration);
return service;
}

特别是在这种情况下,我将从这些模板构建电子邮件。我希望能够将 @section 用于电子邮件的主题和电子邮件正文的不同部分,同时使用我指定整个电子邮件结构通用样式的布局(这可能看起来像 MailChimp 之一)。

那么问题是双重的:

  • 如何在 RazorEngine 中指定布局?
  • 如何从字符串(或流)中指定这些布局?因为如您所见,我使用嵌入式资源来存储 Razor 电子邮件模板。

更新

也许我不清楚,但我指的是 RazorEngine图书馆。

最佳答案

经过一些挖掘后发现支持布局,我们只需要用 _Layout 而不是 Layout 声明它们

关于嵌入资源的问题,我实现了如下ITemplateResolver

using System;
using System.IO;
using System.Reflection;
using Bruttissimo.Common;
using RazorEngine.Templating;

namespace Website.Extensions.RazorEngine
{
/// <summary>
/// Resolves templates embedded as resources in a target assembly.
/// </summary>
public class EmbeddedTemplateResolver : ITemplateResolver
{
private readonly Assembly assembly;
private readonly Type type;
private readonly string templateNamespace;

/// <summary>
/// Specify an assembly and the template namespace manually.
/// </summary>
/// <param name="assembly">The assembly where the templates are embedded.</param>
/// <param name="templateNamespace"></param>
public EmbeddedTemplateResolver(Assembly assembly, string templateNamespace)
{
if (assembly == null)
{
throw new ArgumentNullException("assembly");
}
if (templateNamespace == null)
{
throw new ArgumentNullException("templateNamespace");
}
this.assembly = assembly;
this.templateNamespace = templateNamespace;
}

/// <summary>
/// Uses a type reference to resolve the assembly and namespace where the template resources are embedded.
/// </summary>
/// <param name="type">The type whose namespace is used to scope the manifest resource name.</param>
public EmbeddedTemplateResolver(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
this.assembly = Assembly.GetAssembly(type);
this.type = type;
}

public string Resolve(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
Stream stream;
if (templateNamespace == null)
{
stream = assembly.GetManifestResourceStream(type, "{0}.cshtml".FormatWith(name));
}
else
{
stream = assembly.GetManifestResourceStream("{0}.{1}.cshtml".FormatWith(templateNamespace, name));
}
if (stream == null)
{
throw new ArgumentException("EmbeddedResourceNotFound");
}
string template = stream.ReadFully();
return template;
}
}
}

然后你只需像这样连接它:

    internal static ITemplateService InstanceTemplateService()
{
TemplateServiceConfiguration configuration = new TemplateServiceConfiguration
{
Resolver = new EmbeddedTemplateResolver(typeof(EmailTemplate))
};
ITemplateService service = new TemplateService(configuration);
return service;
}

您传递的类型仅用于引用嵌入资源的程序集和命名空间。

namespace Website.Domain.Logic.Email.Template
{
/// <summary>
/// The purpose of this class is to expose the namespace of razor engine templates in order to
/// avoid having to hard-code it when retrieving the templates embedded as resources.
/// </summary>
public sealed class EmailTemplate
{
}
}

最后一件事,为了让我们的解析器解析模板,我们必须像这样解析它们:

ITemplate template = templateService.Resolve(templateName, model);
string body = template.Run();
return body;

.Run 只是一个简单的扩展方法,因为我找不到 ViewBag 的任何用途。

public static class ITemplateExtensions
{
public static string Run(this ITemplate template)
{
ExecuteContext context = new ExecuteContext();
string result = template.Run(context);
return result;
}
}

更新

这是缺少的扩展

    public static string FormatWith(this string text, params object[] args)
{
return string.Format(text, args);
}

public static string ReadFully(this Stream stream)
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}

关于c# - RazorEngine 字符串布局和部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10369519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com