Skip to content

Commit 5b10fd1

Browse files
authored
Change code language (#35585)
1 parent c6bf58d commit 5b10fd1

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

aspnetcore/mvc/views/razor.md

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ When an `@` symbol is followed by a [Razor reserved keyword](#razor-reserved-key
2626

2727
To escape an `@` symbol in Razor markup, use a second `@` symbol:
2828

29-
```cshtml
29+
```razor
3030
<p>@@Username</p>
3131
```
3232

@@ -38,11 +38,10 @@ The code is rendered in HTML with a single `@` symbol:
3838

3939
HTML attributes and content containing email addresses don't treat the `@` symbol as a transition character. The email addresses in the following example are untouched by Razor parsing:
4040

41-
```cshtml
41+
```razor
4242
4343
```
4444

45-
4645
:::moniker range=">= aspnetcore-6.0"
4746

4847
### Scalable Vector Graphics (SVG)
@@ -69,20 +68,20 @@ HTML attributes and content containing email addresses don't treat the `@` symbo
6968

7069
Implicit Razor expressions start with `@` followed by C# code:
7170

72-
```cshtml
71+
```razor
7372
<p>@DateTime.Now</p>
7473
<p>@DateTime.IsLeapYear(2016)</p>
7574
```
7675

7776
With the exception of the C# `await` keyword, implicit expressions must not contain spaces. If the C# statement has a clear ending, spaces can be intermingled:
7877

79-
```cshtml
78+
```razor
8079
<p>@await DoSomething("hello", "world")</p>
8180
```
8281

8382
Implicit expressions **cannot** contain C# generics, as the characters inside the brackets (`<>`) are interpreted as an HTML tag. The following code is **not** valid:
8483

85-
```cshtml
84+
```razor
8685
<p>@GenericMethod<int>()</p>
8786
```
8887

@@ -97,7 +96,7 @@ Generic method calls must be wrapped in an [explicit Razor expression](#explicit
9796

9897
Explicit Razor expressions consist of an `@` symbol with balanced parenthesis. To render last week's time, the following Razor markup is used:
9998

100-
```cshtml
99+
```razor
101100
<p>Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))</p>
102101
```
103102

@@ -115,7 +114,7 @@ The code renders the following HTML:
115114

116115
Explicit expressions can be used to concatenate text with an expression result:
117116

118-
```cshtml
117+
```razor
119118
@{
120119
var joe = new Person("Joe", 33);
121120
}
@@ -127,15 +126,15 @@ Without the explicit expression, `<p>[email protected]</p>` is treated as an email add
127126

128127
Explicit expressions can be used to render output from generic methods in `.cshtml` files. The following markup shows how to correct the error shown earlier caused by the brackets of a C# generic. The code is written as an explicit expression:
129128

130-
```cshtml
129+
```razor
131130
<p>@(GenericMethod<int>())</p>
132131
```
133132

134133
## Expression encoding
135134

136135
C# expressions that evaluate to a string are HTML encoded. C# expressions that evaluate to `IHtmlContent` are rendered directly through `IHtmlContent.WriteTo`. C# expressions that don't evaluate to `IHtmlContent` are converted to a string by `ToString` and encoded before they're rendered.
137136

138-
```cshtml
137+
```razor
139138
@("<span>Hello World</span>")
140139
```
141140

@@ -154,7 +153,7 @@ The HTML is shown in the browser as plain text:
154153
> [!WARNING]
155154
> Using `HtmlHelper.Raw` on unsanitized user input is a security risk. User input might contain malicious JavaScript or other exploits. Sanitizing user input is difficult. Avoid using `HtmlHelper.Raw` with user input.
156155
157-
```cshtml
156+
```razor
158157
@Html.Raw("<span>Hello World</span>")
159158
```
160159

@@ -168,7 +167,7 @@ The code renders the following HTML:
168167

169168
Razor code blocks start with `@` and are enclosed by `{}`. Unlike expressions, C# code inside code blocks isn't rendered. Code blocks and expressions in a view share the same scope and are defined in order:
170169

171-
```cshtml
170+
```razor
172171
@{
173172
var quote = "The future depends on what you do today. - Mahatma Gandhi";
174173
}
@@ -191,7 +190,7 @@ The code renders the following HTML:
191190

192191
In code blocks, declare [local functions](/dotnet/csharp/programming-guide/classes-and-structs/local-functions) with markup to serve as templating methods:
193192

194-
```cshtml
193+
```razor
195194
@{
196195
void RenderName(string name)
197196
{
@@ -214,7 +213,7 @@ The code renders the following HTML:
214213

215214
The default language in a code block is C#, but the Razor Page can transition back to HTML:
216215

217-
```cshtml
216+
```razor
218217
@{
219218
var inCSharp = true;
220219
<p>Now in HTML, was in C# @inCSharp</p>
@@ -225,7 +224,7 @@ The default language in a code block is C#, but the Razor Page can transition ba
225224

226225
To define a subsection of a code block that should render HTML, surround the characters for rendering with the Razor `<text>` tag:
227226

228-
```cshtml
227+
```razor
229228
@for (var i = 0; i < people.Length; i++)
230229
{
231230
var person = people[i];
@@ -244,7 +243,7 @@ The `<text>` tag is useful to control whitespace when rendering content:
244243

245244
To render the rest of an entire line as HTML inside a code block, use `@:` syntax:
246245

247-
```cshtml
246+
```razor
248247
@for (var i = 0; i < people.Length; i++)
249248
{
250249
var person = people[i];
@@ -265,7 +264,7 @@ Razor automatically omits attributes that aren't required. If the value passed i
265264

266265
For example, consider the following Razor markup:
267266

268-
```cshtml
267+
```razor
269268
<div class="@false">False</div>
270269
<div class="@null">Null</div>
271270
<div class="@("")">Empty</div>
@@ -293,7 +292,7 @@ Razor retains `data-` attributes if their values are `null` or `false`.
293292

294293
Consider the following Razor markup:
295294

296-
```cshtml
295+
```razor
297296
<div data-id="@null" data-active="@false"></div>
298297
```
299298

@@ -311,7 +310,7 @@ Control structures are an extension of code blocks. All aspects of code blocks (
311310

312311
`@if` controls when code runs:
313312

314-
```cshtml
313+
```razor
315314
@if (value % 2 == 0)
316315
{
317316
<p>The value was even.</p>
@@ -320,7 +319,7 @@ Control structures are an extension of code blocks. All aspects of code blocks (
320319

321320
`else` and `else if` don't require the `@` symbol:
322321

323-
```cshtml
322+
```razor
324323
@if (value % 2 == 0)
325324
{
326325
<p>The value was even.</p>
@@ -337,7 +336,7 @@ else
337336

338337
The following markup shows how to use a switch statement:
339338

340-
```cshtml
339+
```razor
341340
@switch (value)
342341
{
343342
case 1:
@@ -356,7 +355,7 @@ The following markup shows how to use a switch statement:
356355

357356
Templated HTML can be rendered with looping control statements. To render a list of people:
358357

359-
```cshtml
358+
```razor
360359
@{
361360
var people = new Person[]
362361
{
@@ -371,7 +370,7 @@ The following looping statements are supported:
371370

372371
`@for`
373372

374-
```cshtml
373+
```razor
375374
@for (var i = 0; i < people.Length; i++)
376375
{
377376
var person = people[i];
@@ -382,7 +381,7 @@ The following looping statements are supported:
382381

383382
`@foreach`
384383

385-
```cshtml
384+
```razor
386385
@foreach (var person in people)
387386
{
388387
<p>Name: @person.Name</p>
@@ -392,7 +391,7 @@ The following looping statements are supported:
392391

393392
`@while`
394393

395-
```cshtml
394+
```razor
396395
@{ var i = 0; }
397396
@while (i < people.Length)
398397
{
@@ -406,7 +405,7 @@ The following looping statements are supported:
406405

407406
`@do while`
408407

409-
```cshtml
408+
```razor
410409
@{ var i = 0; }
411410
@do
412411
{
@@ -422,7 +421,7 @@ The following looping statements are supported:
422421

423422
In C#, a `using` statement is used to ensure an object is disposed. In Razor, the same mechanism is used to create HTML Helpers that contain additional content. In the following code, HTML Helpers render a `<form>` tag with the `@using` statement:
424423

425-
```cshtml
424+
```razor
426425
@using (Html.BeginForm())
427426
{
428427
<div>
@@ -442,7 +441,7 @@ Exception handling is similar to C#:
442441

443442
Razor has the capability to protect critical sections with lock statements:
444443

445-
```cshtml
444+
```razor
446445
@lock (SomeLock)
447446
{
448447
// Do critical section work
@@ -453,7 +452,7 @@ Razor has the capability to protect critical sections with lock statements:
453452

454453
Razor supports C# and HTML comments:
455454

456-
```cshtml
455+
```razor
457456
@{
458457
/* C# comment */
459458
// Another C# comment
@@ -469,7 +468,7 @@ The code renders the following HTML:
469468

470469
Razor comments are removed by the server before the webpage is rendered. Razor uses `@* *@` to delimit comments. The following code is commented out, so the server doesn't render any markup:
471470

472-
```cshtml
471+
```razor
473472
@*
474473
@{
475474
/* C# comment */
@@ -509,7 +508,7 @@ Later in this article, the section [Inspect the Razor C# class generated for a v
509508

510509
The `@attribute` directive adds the given attribute to the class of the generated page or view. The following example adds the `[Authorize]` attribute:
511510

512-
```cshtml
511+
```razor
513512
@attribute [Authorize]
514513
```
515514

@@ -538,7 +537,7 @@ For Razor components, `@code` is an alias of [`@functions`](#functions) and reco
538537

539538
The `@functions` directive enables adding C# members (fields, properties, and methods) to the generated class:
540539

541-
```cshtml
540+
```razor
542541
@functions {
543542
// C# members (fields, properties, and methods)
544543
}
@@ -562,7 +561,7 @@ The following code is the generated Razor C# class:
562561

563562
`@functions` methods serve as templating methods when they have markup:
564563

565-
```cshtml
564+
```razor
566565
@{
567566
RenderName("Mahatma Gandhi");
568567
RenderName("Martin Luther King, Jr.");
@@ -589,7 +588,7 @@ The `@implements` directive implements an interface for the generated class.
589588

590589
The following example implements <xref:System.IDisposable?displayProperty=fullName> so that the <xref:System.IDisposable.Dispose*> method can be called:
591590

592-
```cshtml
591+
```razor
593592
@implements IDisposable
594593
595594
<h1>Example</h1>
@@ -607,7 +606,7 @@ The following example implements <xref:System.IDisposable?displayProperty=fullNa
607606

608607
The `@inherits` directive provides full control of the class the view inherits:
609608

610-
```cshtml
609+
```razor
611610
@inherits TypeNameOfClassToInheritFrom
612611
```
613612

@@ -662,13 +661,13 @@ The `@layout` directive specifies a layout for routable Razor components that ha
662661

663662
The `@model` directive specifies the type of the model passed to a view or page:
664663

665-
```cshtml
664+
```razor
666665
@model TypeNameOfModel
667666
```
668667

669668
In an ASP.NET Core MVC or Razor Pages app created with individual accounts, `Views/Account/Login.cshtml` contains the following model declaration:
670669

671-
```cshtml
670+
```razor
672671
@model LoginViewModel
673672
```
674673

@@ -680,7 +679,7 @@ public class _Views_Account_Login_cshtml : RazorPage<LoginViewModel>
680679

681680
Razor exposes a `Model` property for accessing the model passed to the view:
682681

683-
```cshtml
682+
```razor
684683
<div>The Login Email: @Model.Email</div>
685684
```
686685

@@ -693,7 +692,7 @@ The `@namespace` directive:
693692
* Sets the namespace of the class of the generated Razor page, MVC view, or Razor component.
694693
* Sets the root derived namespaces of a pages, views, or components classes from the closest imports file in the directory tree, `_ViewImports.cshtml` (views or pages) or `_Imports.razor` (Razor components).
695694

696-
```cshtml
695+
```razor
697696
@namespace Your.Namespace.Here
698697
```
699698

@@ -895,7 +894,7 @@ Component references (`@ref`) provide a way to reference a component instance so
895894

896895
Razor templates allow you to define a UI snippet with the following format:
897896

898-
```cshtml
897+
```razor
899898
@<tag>...</tag>
900899
```
901900

@@ -908,7 +907,7 @@ public class Pet
908907
}
909908
```
910909

911-
```cshtml
910+
```razor
912911
@{
913912
Func<dynamic, object> petTemplate = @<p>You have a pet named <strong>@item.Name</strong>.</p>;
914913
@@ -923,7 +922,7 @@ public class Pet
923922

924923
The template is rendered with `pets` supplied by a `foreach` statement:
925924

926-
```cshtml
925+
```razor
927926
@foreach (var pet in pets)
928927
{
929928
@petTemplate(pet)
@@ -940,7 +939,7 @@ Rendered output:
940939

941940
You can also supply an inline Razor template as an argument to a method. In the following example, the `Repeat` method receives a Razor template. The method uses the template to produce HTML content with repeats of items supplied from a list:
942941

943-
```cshtml
942+
```razor
944943
@using Microsoft.AspNetCore.Html
945944
946945
@functions {
@@ -968,7 +967,7 @@ Using the list of pets from the prior example, the `Repeat` method is called wit
968967
* Number of times to repeat each pet.
969968
* Inline template to use for the list items of an unordered list.
970969

971-
```cshtml
970+
```razor
972971
<ul>
973972
@Repeat(pets, 3, @<li>@item.Name</li>)
974973
</ul>

0 commit comments

Comments
 (0)