Requirements: In ASP.NET Core MVC, view the html code output by the template is actually the result rendered by the Template Engine, which is a technology that combines template files (including static content and dynamic placeholders) with data (variables or objects) to generate the final document. It is widely used in various fields such as web development, mail system, report generation, profile generation, etc.
C# Template Engine: RazorEngine(The code hasn't been updated in 8 years, and the latest package is still from 2017)、RazorLight Java Template Engine:Thymeleaf、FreeMarker
RazorLight
Use Razor to build templates from files/embedded resources/strings/databases, or custom sources outside of ASP.NET MVC. No redundant dependencies and workarounds, excellent performance, and support. NET Standard 2.0 and . NET Core 3.0。
Source code address:The hyperlink login is visible.
Console test
Based on the .NET 8 console project. First, the RazorLight package is referenced in the project, as follows:
You also need to add the PreserveCompilationContext configuration, as follows:
Otherwise, an error is reported:Unhandled exception. RazorLight.RazorLightException: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file
In this article, we use the method of embedding resources to add a template file, create a new email folder, and create a new Send.cshtml file, as follows:
The code is as follows:
Tested a normal string, looped through the collection, and added HTML content, as shown in the following figure:
If you want to disable encoding for the entire document - just set the "DisableEncoding" variable to true, the code is as follows:
(End) |