...

/

Razor Component Page-Scoped Keywords

Razor Component Page-Scoped Keywords

Learn page-scoped keywords supported by Razor components.

In this lesson, we’ll cover the individual Razor keywords supported by Razor components in Blazor, focusing on the keywords that apply functionality to an entire page. We have examples of their usage in the project setup below.

This is a modified version of the standard Blazor WebAssembly project template. We’ve added several examples of various Razor keywords to the pages to demonstrate how these keywords work.

<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
            <p role="alert">Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>
Razor components demonstration setup

Razor syntax has the following component-scope keywords that can be applied inside the Blazor application.

@using

This is equivalent to the using keyword in C# and is used for importing namespaces. In C#, namespaces are used for organizing objects inside libraries and subfolders of the libraries. Therefore, when we import a namespace via the using keyword, we gain access to all object types defined under this specific namespace.

When the @using directive is applied in an individual Razor component, the code inside the component gets access to objects from a specific .NET namespace. It works exactly as the using keyword works in raw C#. However, when it’s used inside the _Imports.razor file that can be found in the root folder of the project, the namespaces referenced in the file become accessible to all Razor components inside the application.

@implements

As the C# code inside a ...