Search code examples
razorengine

razorengine - run without compiling into dynamic assembly


In previous versions it was possible to use Parse() for the job.

See old documentation at https://razorengine.codeplex.com/

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });

Sexy. Now however there is only Run() and RunCompile(). And for Run() to work the template needs to be compiled already - I cannot pass in the template only the template's name.

As I do have razor templates where nearly no re-use is happening I would like to be able to NOT have to compile those templates into dynamic assemblies and load them into the app domain. The temporary files issue is another reason.

Is this still possible using RazorEngine?


Solution

  • NOT have to compile those templates into dynamic assemblies

    Is this still possible using RazorEngine?

    No and it never was. Parse will compile the template, load and execute it exactly as RunCompile does. Even worse Parse doesn't cache the template so the loaded assembly is not even accessible (=leaked memory)

    I cannot pass in the template only the template's name

    You can via an extension method if you add using RazorEngine.Templating to the top of your file. Than you can use one of the "RunCompile" examples of the quickstart: https://github.com/Antaris/RazorEngine#quickstart

    I do have razor templates where nearly no re-use is happening

    You should take a look at the isolation API and restart the isolated AppDomain from time to time. On the other hand (if you don't want to pay the cost of AppDomain-boundary) you can handle AppDomain restarts yourself and use the regular API.

    Hope this helps,

    matthid (Disclosure: RazorEngine contributor)