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 8341065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:21:28+00:00 2026-06-09T05:21:28+00:00

I am using the Razor engine https://github.com/Antaris/RazorEngine to parse the body of my email

  • 0

I am using the Razor engine https://github.com/Antaris/RazorEngine to parse the body of my email templates. Is it possible to define a layout and include other .cshtml files? for example a common header and a footer.

  • 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-09T05:21:29+00:00Added an answer on June 9, 2026 at 5:21 am

    I got common templates and a layout working, with the help of these two posts:

    RazorEngine string layouts and sections?

    http://blogs.msdn.com/b/hongyes/archive/2012/03/12/using-razor-template-engine-in-web-api-self-host-application.aspx

    This is my solution:

    Solution 1:
    Layout

    Used by setting _Layout

    @{
        _Layout = "Layout.cshtml";
        ViewBag.Title = Model.Title;
     }
    

    Footer

    @section Footer 
    {
       @RenderPart("Footer.cshtml")
    }
    

    Layout.cshtml

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>
        <head>
        </head>
        <body>
            <div id="content">
                @RenderBody()
            </div> 
            @if (IsSectionDefined("Footer"))
            { 
                <div id="footer">
                    @RenderSection("Footer")
                </div>
            }
        </body> 
    </html>
    

    TemplateBaseExtensions

    Extend TemplateBase with a RenderPart Method

    public abstract class TemplateBaseExtensions<T> : TemplateBase<T>
    {
        public string RenderPart(string templateName, object model = null)
        {
            string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Templates", templateName);
            return Razor.Parse(File.ReadAllText(path), model);
        }
    }
    

    Razor Config

    Set BaseTemplateType to your TemplateBaseExtensions class

    TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration
    {
         BaseTemplateType = typeof(TemplateBaseExtensions<>)
    };
    
    Razor.SetTemplateService(new TemplateService(templateConfig));
    

    Edit Solution 2:

    If you are using a TemplateResolver. RenderPart isn’t needed use the @Include instead

    Footer

    @section Footer 
    {
       @Include("Footer.cshtml")
    }
    

    Resolver

    public class TemplateResolver : ITemplateResolver
    {
        public string Resolve(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
    
            string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Templates", name);
            return File.ReadAllText(path, System.Text.Encoding.Default);
        }
    }
    

    Config

    TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration
    {
         Resolver = new TemplateResolver()
    };
    Razor.SetTemplateService(new TemplateService(templateConfig));
    

    Update by The Muffin Man
    Specify a template and render a string

    var templateResolver = Razor.Resolve("Registration.cshtml");
    return templateResolver.Run(new ExecuteContext());
    

    Also I, along with others at this link https://github.com/Antaris/RazorEngine/issues/61 had issues with using _Layout whereas Layout worked.

    ‘_Layout’ is the old syntax. It was updated to ‘Layout’ in a future release.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing a lot of people using the Razor view engine for email templates.
I'm using ASP.NET MVC3 with razor engine.I want to apply css class on body
I am working on MVC3, I am using Razor engine i have a layout.cshtml
I'm using MVC3's Razor engine to generate views and have the following line of
Using a Telerik Grid with ASP.NET MVC 3 Razor engine. When a user selects
I am new in MS MVC3.I am using C# with razor view engine in
I am using mvc3 and razor is my view engine how i get date
I am using Mvc3 and my view engine is razor and also writing jquery
I am building wizard step demo application with MVC3 and using razor view engine
I am using MVC 3 with Razor Engine. I'm trying to implement an actionlink

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.