- Razor supports C# - It uses the @ symbol to transition from HTML to C#. - When an @ symbol is followed by a Razor reserved keyword, it transitions into Razor-specific markup. Otherwise, it transitions into plain HTML. - To escape an @ symbol in Razor markup, use a second @ symbol:
< p>@@Username< /p>
-Implicit Razor expressions Implicit Razor expressions start with @ followed by C# code:
< p>@DateTime.Now< /p>
< p>@DateTime.IsLeapYear(2016)< /p>
- Implicit expressions cannot contain C# generics, as the characters inside the brackets (< >) are interpreted as an HTML tag. -Explicit Razor expressions Explicit Razor expressions consist of an @ symbol with balanced parenthesis. - Example 1:
< p>Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))< /p>
- Exaample 2:
@{
var joe = new Person("Joe", 33);
}
< p>Age@(joe.Age)< /p>
-Explicit expressions can be used to render output from generic methods in .cshtml files.
< p >@(GenericMethod< int >())< /p >
-Expression encoding
@("< span >Hello World< /span >") // Encodes to : <span>Hello World</span>
- HtmlHelper.Raw output isn't encoded but rendered as HTML markup.
@Html.Raw("< span >Hello World< /span >") //< span >Hello World< /span >
-Razor code blocks Razor code blocks start with @ and are enclosed by {}.
@{
var quote = "The future depends on what you do today. - Mahatma Gandhi";
}
< p>@quote< /p>
// renders: < p>The future depends on what you do today. - Mahatma Gandhi< /p>
- In code blocks, declare local functions with markup to serve as templating methods:
@{
void RenderName(string name)
{
Name: @name
}
RenderName("Mahatma Gandhi");
RenderName("Martin Luther King, Jr.");
}
// Renders:
< p>Name: < strong>Mahatma Gandhi< /strong>< /p>
< p>Name: < strong>Martin Luther King, Jr.< /strong>< /p>
-Implicit transitions The default language in a code block is C#, but the Razor Page can transition back to HTML:
@{
var inCSharp = true;
Now in HTML, was in C# @inCSharp
}
-Explicit delimited transition To define a subsection of a code block that should render HTML, surround the characters for rendering with the Razor < text> tag:
@for (var i = 0; i < people.Length; i++)
{
var person = people[i];
Name: @person.Name
}
-Explicit line transition To render the rest of an entire line as HTML inside a code block, use @: syntax:
@for (var i = 0; i < people.Length; i++)
{
var person = people[i];
@:Name: @person.Name
}
Conditionals @if, else if, else, and @switch
@if (value % 2 == 0)
{
The value was even.
}
else if (value >= 1337)
{
The value is large.
}
else
{
The value is odd and small.
}
@switch (value)
{
case 1:
The value is 1!
break;
case 1337:
Your number is 1337!
break;
default:
Your number wasn't 1 or 1337.
break;
}
Looping @for, @foreach, @while, and @do while
@{
var people = new Person[]
{
new Person("Weston", 33),
new Person("Johnathon", 41),
...
};
@for (var i = 0; i < people.Length; i++)
{
var person = people[i];
Name: @person.Name
Age: @person.Age
}
@foreach (var person in people)
{
Name: @person.Name
Age: @person.Age
}
@{ var i = 0; }
@while (i < people.Length)
{
var person = people[i];
Name: @person.Name
Age: @person.Age
i++;
}
@{ var i = 0; }
@do
{
var person = people[i];
Name: @person.Name
Age: @person.Age
i++;
} while (i < people.Length);
}
@using In Razor,like C#, a using statement is used to ensure an object is disposed.
@using (Html.BeginForm())
{
< div>
Email: < input type="email" id="Email" value="">
< button>Register< /button>
< /div>
}
@try, catch, finally
@try
{
throw new InvalidOperationException("You did something invalid.");
}
catch (Exception ex)
{
The exception message: @ex.Message
}
finally
{
The finally statement.
}
@inherits @inherits TypeNameOfClassToInheritFrom @inject The @inject directive enables the Razor Page to inject a service from the service container into a view.