Using the view engine can solve some problems that require customized content formats, such as email templates.
Quote
install-package RazorEngine
use
public class TestDemo { private string name; public int Age { get => age; set => age = value; } public string Name { get => name; set => name = value; } private int age; }
string template = @" @if(Model!=null){ foreach(var item in Model){ <text>Name:@,age:@</text> } }else{ <text>No data</text> } "; var data = new List<TestDemo>() { new TestDemo(){Name="Zhang San",Age=10 }, new TestDemo(){Name="Zhang Si",Age=11 }, new TestDemo(){Name="Ma Wu",Age=30 } }; var html= (template, data); //Output //Name: Zhang San, Age: 10 //Name: Zhang Si, Age: 11 //Name: Ma Wu, age: 30 var html1 = (template, null); //Output //No data
Supplement: Let's take a look at the syntax of the Razor template engine (C# version)
Razor template engine syntax
1. Introduction:
Razor is a tag syntax that allows you to embed server-based code (Visual Basic and C#) into web pages.
Server-based code can create dynamic web content when a web page is delivered to the browser. When a web page is requested, the server executes the server-based code in the page before returning to the browser. Through the operation of the server, the code can perform complex tasks, such as entering the database.
Razor is based and designed to create web applications. It has traditional features, but is easier to use and easier to learn.
2. Principle:
Razor is a simple programming syntax for embedding server code into web pages.
The Razor syntax is based on a framework, specifically used to create partial frameworks for web applications.
Razor syntax supports all features, but uses a simplified syntax that is easier to learn for beginners and more efficient for experts.
A Razor web page can be described as an HTML web page with two types of content: HTML content and Razor code.
When the server reads a page, it first runs the Razor code and then sends the HTML page to the browser. Code executed on the server can perform some tasks that cannot be completed on the browser, such as accessing the server database. Server code creates dynamic HTML content and then sends it to the browser. From a browser, the HTML generated by the server code is no different from static HTML content.
3. Syntax rules:
The Razor code block is included in @{ ... } . (similar to the <% %> of asp), note that in addition to C# code, html is also allowed in the code block
Inline expressions (variables and functions) start with @ (@ references are required to refer to variables and objects in html)
Code statement ends with a semicolon
Variables are declared using the var keyword (type inference)
Strings are enclosed in quotes
C# code is case sensitive
The extension of the C# file is .cshtml
Syntax Supplement:
1. If foreach while are all code blocks, use @if{} @for(){}, or use @{ if(){} in the code block.
2. Read user input: Request[]; Data conversion: AsInt(), AsDecimal(), etc.
Output non-html and non-variable content in the code block: <text>I am the content I want to output</text> or: @:I am the content I want to output (Variables can also be output in the content @variable names)
Output content in the code block: same in html Use @ variable name (no colon required)
In the call, a method that returns type IHtmlString does not require ";" and does not need to be in the code block. For example: @RenderPage(), @(), etc.
Only using C# code in the code block {} requires ";" Similar to @RenderPage("~/Views/Shared/", new {lxf= "Little Fei Ge"}) does not need to add ";" after the method of outputting content such as @RenderPage("~/Views/Shared/", new {lxf= "Little Fei Ge"})
Output variables in html, such as: @a If there is any content before and after @a, please add brackets: @(a)
This is the article about the simple use of the Razor template engine in C#. For more related content of Razor template engine, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!