The Razor template markup syntax is used throughout ASP.NET. In the next version of ASP.NET, Core Razor is evolving into multiple Razor adaptations such as Razor Tag Helpers, Razor Pages, Blazor, Blazor & Razor Components. In this session, we'll learn the key differences between theses variations and see where they align in the ASP.NET road map. We’ll make sense of how each concept was introduced and what use cases they fulfill.
10. <ul>
<li *ngFor="let p of person">
{{c.name}}
</li>
</ul>
Angular
Compare
<ul>
@foreach (p in people)
{
<li>@p.Name</li>
}
</ul>
Razor
11. @{
var greeting = "Hello";
}
<p>@greeting</p>
@{
greeting = "Howdy";
}
<p>@greeting</p>
IN
Razor Syntax: Code Blocks
<p>Hello</p>
<p>Howdy</p>
Out: HTML
12. <span>@( 5 + 10 + 20 )</span>
IN
Razor Syntax: Complex Expression
<span>35</span>
Out: HTML
13. @if (value % 2 == 0)
{
<p>The value was even.</p>
}
else if (value >= 1337)
{
<p>The value is large.</p>
}
else
{
<p>The value is odd and small.</p>
}
cshtml
Razor Syntax: @if
14. Razor Syntax: Control structures
Keyword
Try catch
While
For
Foreach
If
Do while
Ternary (?: operator)
15. Razor Syntax: Directives
@using adds C# using directive to view
@inherits view class
@inject inject service
@functions add C# code block to a view
16. Razor Complexities
Rendering
• Generating C# code from the template
• Compiling the C# code into an assembly
• Loading the assembly into memory
• Executing your compiled template
17. // Not real code
var engine = new Razor();
var result = engine.RenderTemplate(path);
Fail
Razor For Templates?
21. Razor views (cshtml)
• Streamlined for code-focused templating
• Returned from a controller Action as a ViewResult
• Content is made of
– HTML
– HTML Helpers *typical
– Tag Helpers *new
– Razor Components *bleeding edge
23. Razor Syntax: Directives
@model model type for view
@section enable views to render content to
parts of page
@addTagHelper adds a Tag Helper to scope (.NET Core)
33. Razor Pages
• Introduced in ASP.NET Core
• Page-focused approach incorporates its own
Controller/Action/Routing
• Can integrate with MVC
• Content is made of:
– HTML
– Tag Helpers *typical
– HTML Helpers *compatible
– Razor Components *bleeding edge
38. Tag Helpers
• Async server-side processing via HTML elements
• Use HTML’like Tags and Attributes
• Eliminate context switching between HTML and C#
• Designed for Unit Testing
39. @Html.Label(“Name”, “First Name:”, new { @class = “caption” })
Signature
HTML Helpers vs. Tag Helpers
<label asp-for=“Name” class=“caption”>First Name:</label>
Example
41. Tag Helper Directives
@addTagHelper Component, NameSpace Adds a Tag Helper to scope
@addTagHelper *, Kendo.Mvc Ex: 3rd party Tag Helpers
@removeTagHelper Component, Namespace Removes a Tag Helper from scope
@tagHelperPrefix th: Prefixes Tag Helpers ex: <th:myWidget>
42. Built-in ASP.NET Core Tag Helpers
• Anchor
• Cache
• Distributed Cache
• Environment
• Form
• Form Action
• Image
• Input
• Label
• Partial
• Select
• Textarea
• Validation Message
• Validation Summary
43. Tag Helpers: Example
<kendo-datepicker name="startDate"
start="CalendarView.Year"
depth="CalendarView.Year"
format="MMMM yyyy"
value='DateTime.Parse("May 2019")' />
46. View Components
• Renders a chunk rather than a whole response
• Similarities with controller and view
• Can have parameters and business logic
• Is typically invoked from a layout page
• Deprecated by Razor Components??
47. List Items From Database
<vc:priority-list max-priority="2"
is-done="false">
</vc:priority-list>
49. Razor Component (Blazor Framework)
• Razor Syntax
• Component Architecture vs. HTML generator
• Creates a RenderTree (DOM Abstraction)
• Similar to JSX for React
• Uses .razor file extension
• .razor files are used for code generation of .NET
classes
– These classes implement a RenderTree
52. PUBLISHING
COMPONENT MODEL FORMS & VALIDATION
DEPENDENCY INJECTION
AUTO REBUILDUNIT TESTING
JAVASCRIPT INTEROP
SERVER-SIDE RENDERING
DEBUGGING
INTELLISENSE & TOOLING
LAYOUTS
ASSEMBLY TRIMMING
COMPONENT PACKAGES
ROUTING
Blazor
53. • What it is
• Client Side C#
• Web Standard Technologies
• Mono Runtime for
WebAssembly
• .NET Standard
• What it’s NOT
• A Plugin
• ASP.NET WebForms
• Silverlight
• Trans-piled JavaScript
I’m a developer advocate for Progress
We make UI components for everything
The default Razor language is HTML. Rendering HTML from Razor markup is no different than rendering HTML from an HTML file. HTML markup in .cshtml Razor files is rendered by the server unchanged.
Razor supports C# and uses the @ symbol to transition from HTML to C#. Razor evaluates C# expressions and renders them in the HTML output.
When an @ symbol is followed by a Razor reserved keyword, it transitions into Razor-specific markup. Otherwise, it transitions into plain C#.
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.
Another example from Telerik UI for ASP.NET Core. This is the Tag Helper for the Date Picker.
The Tag Helper is using attribute values that are strong-typed.
Build rich, interactive client UI without writing JavaScript
Create reusable UI components with C# and Razor
Integrates with your existing ASP.NET Core applications
Blazor apps are based on components. A component in Blazor is an element of UI, such as a page, dialog, or data entry form. Components handle user events and define flexible UI rendering logic. Components can be nested and reused.
Components are .NET classes built into .NET assemblies that can be shared and distributed as NuGet packages. The component class is usually written in the form of a Razor markup page with a .razor file extension.
Components in Blazor are sometimes referred to as Razor components. Razor is a syntax for combining HTML markup with C# code designed for developer productivity. Razor allows you to switch between HTML markup and C# in the same file with IntelliSense support. Razor Pages and MVC also use Razor. Unlike Razor Pages and MVC, which are built around a request/response model, components are used specifically for client-side UI logic and composition.
Opens a gateway to the language multiverse
Finally let’s talk Blazor
A different approach
Blazor is a fully featured single page application framework by Microsoft
It has a huge ecosystem of .NET packages on NuGet because it’s compatible with .NET standard
Even Telerik has a UI components
Mono Runtime for web assembly
.NET Core 3 will ship in September
.NET Core 3.1 will have long-term support
.NET 5 release in November 2020
Major releases every year, long-term support for even-numbered releases
Predictable schedule, minor releases if needed