Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Razor template and Razor Engine

0.00/5 (No votes)
20 Nov 2011 1  
Using Razor template outside MVC project using Razor Engine

Introduction 

We can use Razor template outside MVC project as a content template and generate the real content using RazorEngine  

RazorEngine

A templating engine built upon Microsoft's Razor parsing technology. The RazorEngine allows you to use Razor syntax to build robust templates. Download RazorEngine here( http://razorengine.codeplex.com/

Define Razor Template

TestTemplate.cshtml
<h1>@Model.DisplayName</h1>

Message

@Model.Message

Generate the Real content using RazorEngine 

using RazorEngine;
using System.Dynamic;

namespace HelloConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            // read the template
            var template = File.ReadAllText(@"TestTemplate.cshtml");

            // create template data
            dynamic myObject = new { DisplayName = "Mahesh", 
			          Message="Sample Message" };            

            // generate the content using razor engine
            string result = Razor.Parse(template,myObject);

            // display the result
            Console.WriteLine(result);

            Console.ReadLine();
        }
    }
} 

 

Conclusion

I hopes you got some idea about how to use razor template outside mvc project using RazorEngine. This is the same as my original post Razor template outside MVC project.  

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here