toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.51k stars 259 forks source link

Sample code in readme crashes due to null RazorLightProject #286

Closed bezysoftware closed 4 years ago

bezysoftware commented 4 years ago

In beta2 it seems impossible to use the EngineBuilder without specifying a RazorLightProject. This should not be required as I can pass a template as a string in parametr

jonny64bit commented 4 years ago

I'm facing the same issue. As a work around use "UseFileSystemProject" and point it to an empty folder.

var engine = new RazorLightEngineBuilder().UseMemoryCachingProvider() .UseFileSystemProject(emptyFolderPath).Build();

jzabroski commented 4 years ago

@bezysoftware Are you referring to this snippet?

var engine = new RazorLightEngineBuilder()
              .UseMemoryCachingProvider()
              .Build();

string template = "Hello, @Model.Name. Welcome to RazorLight repository";
ViewModel model = new ViewModel() { Name = "John Doe" };

string result = await engine.CompileRenderAsync("templateKey", template, model);
bezysoftware commented 4 years ago

Yep, that's the one. I actually resolved the issue by subclassing RazorLightProject (with implementations throwing NotImplementedException) and registering that with the builder. Since I provide the template as a string parameter, the flow never reaches this code and no exception is thrown. @jonny64bit 's solutions will probably work as well

jzabroski commented 4 years ago

Fixed. Please re-open if i missed something.

@bezysoftware Any chance you'd be willing to add code coverage for a UseStringProject() and a RazorLightStringProject class? I think that is the direction to go in, based on your feedback.