Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7919913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:01:39+00:00 2026-06-03T16:01:39+00:00

I use razor engine like this: public class EmailService : IService { private readonly

  • 0

I use razor engine like this:

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;
    }
}

The templating service I use is injected although it’s just a default implementation:

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

Since in this case in particular I will be building emails from these templates. I want to be able to use @sections for the email’a subject, and different sections of the email body, while using a layout where I specify the styles that are common to the whole email structure (which will look like one of MailChimp‘s probably).

The question is then twofold:

  • How can I specify layouts in RazorEngine?
  • How can I specify these layouts from strings (or a stream)? since as you can see, I use embedded resources to store the razor email templates.

Update

Maybe I wasn’t clear, but I’m referring to the RazorEngine library.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:01:41+00:00Added an answer on June 3, 2026 at 4:01 pm

    Turns out after some digging that layouts are supported, we just have to declare them with _Layout instead of Layout

    As for the embedded resource issue, I implemented the following 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;
            }
        }
    }
    

    Then you just wire it like this:

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

    The type you pass is just for referencing the assembly and namespace where the resources are embedded.

    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
        {
        }
    }
    

    One last thing, in order to have the templates resolved with our resolver we have to resolve them like this:

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

    .Run is just a simple extension method since I can’t find any use for a ViewBag.

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

    UPDATE

    Here are the missing extensions

        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();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use razor syntax inside my Javascript files. Is this possible without
I'd like to use the Razor engine without view (cshtml) files, but on strings.
I'd like to use the Razor View Engine outside of ASP.NET MVC to generate
Possible Duplicate: Escape @ character in razor view engine how can i use '@'
Is there any documentation on how to use the Razor view engine using ASP.NET
I'm using ASP.NET MVC3 with razor engine.I want to apply css class on body
Is it possible to use Razor View Engine (ASP.NET MVC) together with .LESS (similar
I would like to generate emails using the Razor view engine. From what I've
I am using asp.net mvc3 razor engine and trying to use the MVCSiteMap provider
I use Valums to upload large files under MVC3 Razor. Somehow It uploads only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.