This article is a mirror article of machine translation, please click here to jump to the original article.

View: 654|Reply: 1

[Console Program] RazorLight tutorial for the .NET/C# template engine

[Copy link]
Posted on 2025-8-4 10:14:30 | | | |
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:ThymeleafFreeMarker

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)




Previous:Linux uses rsync to synchronize data to the new server
Next:SQL SERVER database modifies the default collation rule
 Landlord| Posted on 2025-8-8 10:24:31 |
RazorLight is registered and used in ASP.NET Core dependency injection
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com