Click here to Skip to main content
Licence CPOL
First Posted 13 Aug 2011
Views 7,946
Bookmarked 6 times

Razor template and Razor Engine

By | 20 Nov 2011 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

A Mahesh

Web Developer

India India

Member

Follow on Twitter Follow on Twitter
Symphony Services, Banglore, 560 087, India

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 [modified] PinmvpSacha Barber20:01 20 Nov '11  
GeneralMy vote of 1 PinmemberMohammad_Denmark3:52 18 Nov '11  
GeneralMy vote of 1 PinassociateThomas Freudenberg0:15 16 Aug '11  
GeneralMy vote of 1 PinmemberSteven Berkovitz10:22 15 Aug '11  
GeneralRe: My vote of 1 PinmvpSacha Barber20:01 20 Nov '11  
SuggestionBetter suited as a Tip/Trick PinmentorDaveAuld20:38 13 Aug '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 21 Nov 2011
Article Copyright 2011 by A Mahesh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid