A component is a class
A .razor file becomes a C# partial class named after the file, in the namespace given by the project and folder (or by @namespace). Its markup becomes the BuildRenderTree method; @code (and @functions) members become members of the class. A partial class in Name.razor.cs can add more members; a Name.razor.css beside it is CSS scoped to the component.
@* Alert.razor *@
<div class="alert alert-@Kind" role="alert">
@if (Title is not null)
{
<strong>@Title</strong>
}
@ChildContent
</div>
@code {
[Parameter] public string Kind { get; set; } = "info";
[Parameter] public string? Title { get; set; }
[Parameter] public RenderFragment? ChildContent { get; set; }
}
Using a component
A component is used as an element whose name is the component's type name, written with the same case: <Alert />. The name may be qualified with a namespace (<Shop.Controls.Alert />). The namespace must be in scope, by @using in the file or in _Imports.razor, or by the file being in the same namespace. HTML element names are case-insensitive and are normally written in lower case; the scanner treats an element as a component when its name starts with an upper-case letter or contains a dot.
Attributes of a component tag are its parameters, matched to [Parameter] properties by name:
| Written | Meaning |
|---|---|
Kind="warning" | A constant, converted to the parameter's type (string, number, bool, enum ...). |
Count="@n", Count="@(n + 1)" | A C# expression. Title="Hello @name" mixes text and expressions and is a string. |
OnSave="Save" or OnSave="@Save" | A method group for an EventCallback or delegate parameter. |
@bind-Value="x" | A directive attribute: two-way binding. |
Extra="a" Other="b" not declared | Goes to a [Parameter(CaptureUnmatchedValues = true)] dictionary if the component has one, otherwise it is an error. |
Child content and templates
What is written between the start and end tag of a component is passed to its ChildContent parameter (a RenderFragment): <Alert Kind="danger">Something failed</Alert>. A component with several fragments names them with child elements named after the parameters:
<Table Items="people">
<Header><th>Name</th></Header>
<RowTemplate Context="person">
<td>@person.Name</td>
</RowTemplate>
</Table>
A parameter of type RenderFragment<T> receives a value: inside it the implicit parameter is called context, or another name given by a Context="name" attribute on the element (or on the component tag for the child content). In a component's own code, RenderFragment values are written with @name (for a plain fragment) or @name(value) (for a templated one), and a fragment can be built in code with an inline template: RenderFragment hello = @<p>Hello</p>;.
Generic components
@typeparam TItem declares a type parameter of the component class; a constraint may follow: @typeparam TEntity where TEntity : IEntity, where the type named after where must be the one just declared. Callers either let the compiler infer the type from a parameter's value or give it by an attribute named after the type parameter: <Table TItem="Person" Items="people" />. A [Parameter] of type IEnumerable<TItem>, RenderFragment<TItem> and so on uses the parameter.
Members and markup in code
@code { ... } holds C# class members: fields, properties, methods, nested types, attributes such as [Parameter], [CascadingParameter], [Inject]. A component may have more than one @code block. Methods in it may contain markup: the rules for markup in code apply, and a method that contains markup becomes a templating method. The component lifecycle methods (OnInitialized, OnParametersSetAsync, OnAfterRender, SetParametersAsync, Dispose through @implements IDisposable) are ordinary C# in this block and have no Razor syntax of their own.
Cascading values pass through components by type, not by markup: <CascadingValue Value="theme"> ... </CascadingValue> is itself a component whose child content receives the values.
Routing, layouts and render modes
@page "/path/{param}"makes the component a page. The route may have parameters with constraints ({id:int}), and a component may have several@pagelines. The route parameters fill[Parameter]properties of the same name.@layout MainLayoutnames the layout component; a layout has aBodyparameter of typeRenderFragment([Parameter] public RenderFragment? Body, written@Body) and derives fromLayoutComponentBase. Put it in_Imports.razorto apply to a whole folder.@rendermode InteractiveServer(directive) or@rendermode="..."(directive attribute) chooses where the component runs: on the server, in WebAssembly, or automatically.@attribute [Authorize]adds a C# attribute;@injectinjects a service;@inheritssets the base class;@implementsadds interfaces;@preservewhitespace truekeeps white space.
In the viewer
Component tags are coloured as types (the angle bracket as a tag); every distinct component used is listed in the Outline tab with the number of its uses and the line of the first. The scanner cannot know whether a name is a real component, since that depends on the project's assemblies; a capitalised element name that is really an HTML element, written in capitals, is listed too.