Using Razor outside of MVC in .NET Core

后端 未结 6 2117
南旧
南旧 2020-11-27 11:05

I would like to use Razor as a templating engine in a .NET console application that I\'m writing in .NET Core.

The standalone Razor engines I\'ve come across (RazorE

6条回答
  •  失恋的感觉
    2020-11-27 11:24

    Recently I've created a library called RazorLight.

    It has no redundant dependencies, like ASP.NET MVC parts and can be used in console applications. For now it only supports .NET Core (NetStandard1.6) - but that's exactly what you need.

    Here is a short example:

    IRazorLightEngine engine = EngineFactory.CreatePhysical("Path-to-your-views");
    
    // Files and strong models
    string resultFromFile = engine.Parse("Test.cshtml", new Model("SomeData")); 
    
    // Strings and anonymous models
    string stringResult = engine.ParseString("Hello @Model.Name", new { Name = "John" }); 
    

提交回复
热议问题