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)
{
var template = File.ReadAllText(@"TestTemplate.cshtml");
dynamic myObject = new { DisplayName = "Mahesh",
Message="Sample Message" };
string result = Razor.Parse(template,myObject);
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.