Using Razor engine on strings - not viewing

I would like to use the Razor engine without viewing (cshtml) files, but line by line. I want to do this from MVC, I saw examples that use

new RazorViewEngine().Render 

but I canโ€™t find the Render method, is this something from the old days of MVC?

I also saw examples that use Razor.Parse, but I also canโ€™t find it - there may not be a link (but it should be there if I already use MVC, right?)

Is it possible to use Razor at all if all I have to do is enter 3-4 parameters in an HTML string? I think I'm a bit in love with MVC right now and maybe not thinking directly. I plan to cache HTML strings in memory and just transfer samples from the database.

thanks

+7
source share
3 answers

You can take a look at RazorEngine .

+3
source

To use RazorEngine for parsing strings, you will need RazorEngine.dll, which can be downloaded from http://razorengine.codeplex.com/ p>

To parse a string using the Razor mechanism, use the following example:

 var model = new { Name = "Test" }; var template = "Hello @Model.Name"; var result = Razor.Parse(template, model); 

Depending on whether it is advisable to use it to parse a string, it really depends on what you use it for. If you think you need the flexibility that Razor offers, I would recommend it, but when compared to a standard string replacement, it brings a bit of performance.

+5
source

There is a RazorParser class with an analysis method that takes TextReader as an input parameter. However, the whole System.Web.Razor.Parser namespace is marked as

This type / member supports the .NET Framework and does not support intended use directly from your code.

so you need to figure it out yourself.

0
source

All Articles