In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how to use the Razor template engine in C#. The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the Razor template engine in C#.
Using the view engine, you can accomplish some issues that require customized content formats, such as mail templates.
Refer to install-package RazorEngine and 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 (modelling null) {foreach (var item in Model) {name: @ item.Name, age: @ item.Age}} else {no data}" Var data = new List () {new TestDemo () {Name= "Zhang San", Age=10}, new TestDemo () {Name= "Zhang Si", Age=11}, new TestDemo () {Name= "Ma Wu", Age=30}}; var html= Razor.Parse (template, data) / / output / / name: Zhang San, age: 10 / / name: Zhang Si, age: 11 / / name: Ma Wu, age: 30 var html1 = Razor.Parse (template, null); / / output / / No data is available
Add: let's take a look at the syntax of Razor template engine (C # version)
Razor template engine syntax 1. Introduction:
Razor is a markup syntax that allows you to embed server-based code (Visual Basic and C #) into a web page.
Server-based code can create dynamic Web content when a web page is delivered to a browser. When a web page is requested, the server executes the server-based code in the page before returning the page to the browser. Through the operation of the server, the code can perform complex tasks, such as entering the database.
Razor is based on ASP.NET and is designed to create Web applications. It has the functions of traditional ASP.NET, but it is easier to use and easier to learn.
2. Principle:
Razor is a simple programming syntax that embeds server code in a web page.
The Razor syntax is based on the ASP.NET framework and is specifically used to create part of the Microsoft.NET framework for Web applications.
Razor syntax supports all the functions of ASP.NET, but uses a simplified syntax that is easier for beginners to learn and more efficient for experts.
Razor pages can be described as HTML pages with two types of content: HTML content and Razor code.
When the server reads the page, it first runs the Razor code and then sends the HTML page to the browser. Code executed on the server can perform tasks that cannot be done on the browser, such as accessing the server database. The server code can create dynamic HTML content and send it to the browser. From the browser point of view, the HTML generated by the server code is no different from the static HTML content.
3. Grammar rules:
The Razor code block is contained in @ {...}. (similar to asp), note that in addition to the C # code in the code block, html is also allowed
Inline expressions (variables and functions) start with @ (references to variables in html, objects need to use @ references)
The code statement ends with a semicolon
Variables are declared using the var keyword (type inference)
The string is enclosed in quotation marks
C # code is case sensitive
The extension of the C# file is .cshtml
Grammatical supplement:
1. If for foreach while is a code block, used with @ if {} @ for () {}, or in a code block using @ {if () {}}
2. Read user input: Request []; data conversion: AsInt (), AsDecimal (), etc.
Output non-html and non-variable content in the code block: I am the content to be output or: @: I am the content to be output (variable @ variable name can also be output in the content)
Output in the code block: also use the @ variable name in html (no colons required)
You don't need ";" to call a method that returns an IHtmlString type, nor do you need to be in a code block. For example, @ RenderPage (), @ Html.Partial (), etc.
Only output methods like @ RenderPage ("~ / Views/Shared/PartialOne.cshtml", new {lxf= "Xiaofei"}) do not need to be followed by ";"
Html output variables such as: @ a before and after @ a, please add parentheses: @ (a)
Thank you for your reading, the above is the content of "how to use the Razor template engine in C#". After the study of this article, I believe you have a deeper understanding of how to use the Razor template engine in C#, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.