Tuesday, July 8, 2014

Using RazorEngine with layouts outside of ASP.NET MVC app

RazorEngine is library that give us possibility to use Microsoft Razor library outside of ASP.NET MVC. For us it was a great way how to implement email layouts - there are even whole libraries for that if you don’t want to reimplement the wheel (e.g. ActionMailer).

If you decide to use RazorEngine for something more complex that a single view rendering, first bump on the road will probably be layout resolving. It is quite obvious, that RazorEngine supports layouts since TemplateBase class (from which your views should inherit) contains a Layout property. But a naive approach usually ends in a NullReferenceException trap (as can be seen in this issue).

The thing that got me back on track realizing that this is not a bug in library was this blogpost. I have reproduced the steps in there and then dug deeper in the source code of the library to find out, what went wrong in the first place.

First it is important to realize that RazorEngine is just a way how to compile a view. It is not a full featured view engine that is be capable of resolving layouts from files for us (when layout is specified). When you ask RazorEngine to work with a view, it compiles it and saves it in the local cache (defined as a ConcurrentDictionary in TemplateService). If the view is linked to other views (layouts, partial views...) it just looks for the views in the cache by their name. Thus the only thing, that you need to do when creating a view with a layout is compiling the layout before the view and then linking the layout from the view by the same name as is the cache key.

Let me show you an example. First you need to install RazorEngine package from NuGet.

var layout = "<html><head><title>Page with layout</title></head><body>@RenderBody()</body></html>";
var content = "@{ Layout = \"Layout\"; } content string";

Razor.Compile(layout, "Layout");
var resultView = Razor.Parse(content);

And the result saved in the result variable is

<html><head><title>Page with layout</title></head><body> <span>content string</span></body></html>

The important thing here is - you have to compile the layout before compiling/parsing the view. Also the name, that you pass as a second parameter of the Razor.Compile method must be the same as a Layout name mentioned in the view (in this example it was "Layout"). If you don’t play by these rules, you’ll get NullReferenceException (or more meaningful exception thanks to my accepted pull request).

22 comments:

  1. Replies
    1. I have read your blog its very attractive and impressive. I like it your blog.

      Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai


      Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

      Delete
  2. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.

    SAP training in Chennai

    ReplyDelete
  3. Great article. Glad to find your blog. Thanks for sharing.

    web designing training in chennai

    ReplyDelete
  4. Great Information,it has lot for stuff which is informative.I will share the post with my friends.
    Case Study Writing Services

    ReplyDelete
  5. MARKETING CASE STUDY HELP & MARKETING HELP Marketing Case Study Help Opportunity to all student and different researchers. please feel free to visit Marketing Help Case Study

    ReplyDelete
  6. Get your Finance Assignment Help, Homework, Project Help Online for Students in UK, USA, and Australia by Professional finance writers at affordable prices. Finance Assignments Help

    ReplyDelete

  7. i was just browsing along and came upon your blog. just wanted to say good blog and this article really helped me.
    Cross-platform app

    ReplyDelete
  8. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Business Administration Summer Camp in Velachery | Business Administration Summer Classes in Chennai | Business Administration Summer courses in Taramani

    ReplyDelete
  9. Very Useful information that i have found. don't stop and Please keep updating us..... Thanks
    Summer Courses in Perungudi | Summer Courses in OMR | Summer Courses in Velachery

    ReplyDelete
  10. Awesome blog with smart content. Rally this blog was very helpful to us. I referred by friends also, thanks for sharing such a nice blog..
    Good Summer Courses for Hardware and Networking in Chennai | Perfect Vacation Courses in Chennai

    ReplyDelete
  11. Impressive blog with lovely information. really very useful article for us thanks for sharing such a wonderful blog...
    Summer Camp for Kids in Chennai | Summer courses in Shozhinganallur | Summer classes in Chennai

    ReplyDelete
  12. I really love reading and following your post as I find them extremely informative and interesting. This post is equally informative as well as interesting Summer Camp for Kids in Chennai | Summer courses in Shozhinganallur | Summer classes in Chennai

    ReplyDelete
  13. Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating... Summer Camp for Computer Science in Velachery | Summer classes for Computer Science in Chennai | Summer Courses for Computer Science in Taramani

    ReplyDelete
  14. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Summer Courses for Android in Perungudi | Summer Courses for IOS in Velachery | Summer Courses in OMR

    ReplyDelete
  15. Impressive blog with lovely information. really very useful article for us thanks for sharing such a wonderful blog... 
    Summer Courses in Perungudi | Summer Courses in OMR | Summer Courses in Velachery

    ReplyDelete
  16. Really its a very good article, thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic. Graphic Designing Summer Courses in Velachery | Graphic Designing Summer Courses in Chennai | Graphic Designing Summer Courses in Taramani

    ReplyDelete
  17. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information, this is useful to me…
    Software Testing Summer Courses in Adyar | Summer Courses in Velachery | Java Summer Courses in Perungudi

    ReplyDelete