Blazor
Razor
Razor is a markup syntax for embedding .NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a .cshtml file extension. Razor is also found in Razor component files (.razor). Razor syntax is similar to the templating engines of various JavaScript single-page application (SPA) frameworks, such as Angular, React, VueJs, and Svelte.
C#
Razor supports C# and uses the @ symbol to transition from HTML to C#. Razor evaluates C# expressions and renders them in the HTML output. To escape an @ symbol in Razor markup, use a second @ symbol. HTML attributes and content containing email addresses don’t treat the @ symbol as a transition character.
<p>@@Username</p>
<a href="mailto:Support@contoso.com">Support@contoso.com</a>
@()和@{}
@()直接计算表达式的值,然后渲染在页面上。@{}属于代码段,不会被渲染。但是@{}中代码会被顺序执行。代码块中支持HTML直接输出。
Directives
CSHTML @{ var quote = “Getting old ain’t for wimps! - Anonymous”; }
<div>Quote of the Day: @quote</div>
The code generates a class similar to the following:
C#
public class _Views_Something_cshtml : RazorPage
WriteLiteral("/r/n<div>Quote of the Day: ");
Write(output);
WriteLiteral("</div>");
}
}