7

I'd like to use the Razor engine without view (cshtml) files, but on strings. I want to do it from within MVC, I've seen examples that use

new RazorViewEngine().Render

but I can't find the Render method, is it something from the older days of MVC?

I've also seen examples that use Razor.Parse, but I can't find it either - probably missing a reference (but it should be there if I'm using MVC already, right?)

Is it advisable at all to use Razor if all I need to do is inject 3-4 parameters into an HTML string? I think I'm a bit infatuated with MVC right now and might not be thinking straight. I'm planning to cache the HTML strings in memory and just pass-in models from DB.

Thank you

Madd0g
  • 3,841
  • 5
  • 37
  • 59

3 Answers3

5

In order to use the RazorEngine to parse strings, you will need the RazorEngine.dll, which can be downloaded from http://razorengine.codeplex.com/

To parse a string with the Razor engine just use the following example:

var model = new { Name = "Test" };
var template = "Hello @Model.Name";

var result = Razor.Parse(template, model);

As to whether or not it is advisable to use it for parsing a string, really depends on what you are using it for. If you think you are going to need the flexibility that Razor offers, I would recommend it, but it does come with a bit of a performance hit when comparing it to a standard string replace.

Jamil Geor
  • 337
  • 2
  • 5
  • I'm using it for managing views outside of the project - I do care about performance since I expect many (20? 50?) calls to these templates on a single page-view - that's why their blog post about the changes in the new version make me scared of trying the old one, which they say doesn't work very well in multi-threaded environments (web probably falls under this category, right?) – Madd0g Nov 21 '11 at 18:47
  • Yeah probably not a good idea for what you are trying to achieve. – Jamil Geor Nov 21 '11 at 21:41
3

You may take a look at RazorEngine.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This seems like a bit of an overkill - It feels to me like I should be able to do this without such a huge external library - but it does look very nice. However - looking at their [blog post](http://www.fidelitydesign.net/?p=473) about version 3 makes me not want to try the current version, because I do need to run it under a multithreaded environment and (hopefully) support caching of the template – Madd0g Nov 21 '11 at 18:37
  • @Madd0g, what is your scenario? Maybe there are more adapted things to fit your needs. – Darin Dimitrov Nov 21 '11 at 18:52
  • I have a list of HTML templates with placeholders in them, each is associated with a set of rules, they are saved in a DB. Eventually these templates would be applied to data that meets some conditions (and the rules). Preferably the entire template is cached so it handles data quickly. It needs to work just like a partial-view, only more "dynamically". – Madd0g Nov 21 '11 at 19:12
  • @Madd0g, a custom virtual provider which will fetch views from database instead of the file system seems like a good way to tackle this problem. – Darin Dimitrov Nov 21 '11 at 19:33
  • 1
    Thank you, it looks like the virtual path provider the way to go, from what I've [read](http://rebuildall.umbraworks.net/2009/11/17/ASP_NET_MVC_and_virtual_views) it sounds pretty straight-forward. – Madd0g Nov 21 '11 at 23:02
0

There is a RazorParser class with Parse method which takes TextReader as an input parameter. However whole System.Web.Razor.Parser namespace is marked as

This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

so you have to figure it out by yourself.

Jakub Šturc
  • 35,201
  • 25
  • 90
  • 110