Skip to content

2.0-alpha3

Pre-release
Pre-release
Compare
Choose a tag to compare
@toddams toddams released this 23 Sep 09:34
· 55 commits to dev-2.0 since this release

What is new?

String rendering is back!

We added rendering of the strings back! And now they are cached, so you will not be affected by bad performance.

Note: if you store your templates in database - it is recommended approach to define custom RazorLightProject that fetches templates from database and create RazorLightEngine passing your implementation to it. This way, engine will your RazorLightProject to resolve layouts. String rendering is just another option for some exclusive use cases when you have microservice structure and pass templates between nodes, so you might not need it at all.

Here is an example of RazorLightProject that uses EntityFramework to get templates from database- https://github.com/toddams/RazorLight/blob/dev-2.0/samples/RazorLight.Samples/Program.cs

string templateKey = "key";
string result = await engine.CompileRenderAsync(templateKey , "Hello @Model.Name", new { Name = "Johny" });

//Returns true, so next time you render template with this key - it will not be compiled, but taken from cache
engine.TemplateCache.Contains(templateKey); 

Additional metadata references

When RazorLight compiles your template - it loads all the assemblies from your entry assembly and creates MetadataReference from it. This is a default strategy and it works in 99% of the time. But sometimes compilation crashes with an exception message like "Can not find assembly My.Super.Assembly2000". In order to solve this problem you can pass additional metadata references to RazorLight.

var options = new RazorLightOptions();

var metadataReference = MetadataReference.CreateFromFile("path-to-your-assembly")
options.AdditionalMetadataReferences.Add(metadataReference );

var project = new FileSystemRazorProject("path-to-your-views");
var engine = new EngineFactory().Create(project, options);