Events
Mar 31, 11 PM - Apr 2, 11 PM
The ultimate Microsoft Fabric, Power BI, SQL, and AI community-led event. March 31 to April 2, 2025.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
This isn't the latest version of this article. For the current release, see the .NET 9 version of this article.
Warning
This version of ASP.NET Core is no longer supported. For more information, see the .NET and .NET Core Support Policy. For the current release, see the .NET 9 version of this article.
Important
This information relates to a pre-release product that may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
For the current release, see the .NET 9 version of this article.
Razor components can be integrated into Razor Pages or MVC apps. When the page or view is rendered, components can be prerendered at the same time.
Important
Framework changes across ASP.NET Core releases led to different sets of instructions in this article. Before using this article's guidance, confirm that the document version selector at the top of this article matches the version of ASP.NET Core that you intend to use for your app.
Prerendering can improve Search Engine Optimization (SEO) by rendering content for the initial HTTP response that search engines can use to calculate page rank.
After configuring the project, use the guidance in the following sections depending on the project's requirements:
@page
directive.
Use the following guidance to integrate Razor components into pages or views of an existing Razor Pages or MVC app.
Add an imports file to the root folder of the project with the following content. Change the {APP NAMESPACE}
placeholder to the namespace of the project.
_Imports.razor
:
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using {APP NAMESPACE}
In the project's layout file (Pages/Shared/_Layout.cshtml
in Razor Pages apps or Views/Shared/_Layout.cshtml
in MVC apps):
Add the following <base>
tag and HeadOutlet component Tag Helper to the <head>
element:
<base href="~/" />
<component type="typeof(Microsoft.AspNetCore.Components.Web.HeadOutlet)"
render-mode="ServerPrerendered" />
The href
value (the app base path) in the preceding example assumes that the app resides at the root URL path (/
). If the app is a sub-application, follow the guidance in the App base path section of the Host and deploy ASP.NET Core Blazor article.
The HeadOutlet component is used to render head (<head>
) content for page titles (PageTitle component) and other head elements (HeadContent component) set by Razor components. For more information, see Control head content in ASP.NET Core Blazor apps.
Add a <script>
tag for the blazor.server.js
script immediately before the Scripts
render section (@await RenderSectionAsync(...)
):
<script src="_framework/blazor.server.js"></script>
The framework adds the blazor.server.js
script to the app. There's no need to manually add a blazor.server.js
script file to the app.
Note
Typically, the layout loads via a _ViewStart.cshtml
file.
Register the Blazor Server services in Program.cs
where services are registered:
builder.Services.AddServerSideBlazor();
Add the Blazor Hub endpoint to the endpoints of Program.cs
where routes are mapped. Place the following line after the call to MapRazorPages
(Razor Pages) or MapControllerRoute
(MVC):
app.MapBlazorHub();
Integrate components into any page or view. For example, add a Counter
component to the project's Shared
folder.
Pages/Shared/Counter.razor
(Razor Pages) or Views/Shared/Counter.razor
(MVC):
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Razor Pages:
In the project's Index
page of a Razor Pages app, add the Counter
component's namespace and embed the component into the page. When the Index
page loads, the Counter
component is prerendered in the page. In the following example, replace the {APP NAMESPACE}
placeholder with the project's namespace.
Pages/Index.cshtml
:
@page
@using {APP NAMESPACE}.Pages.Shared
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<component type="typeof(Counter)" render-mode="ServerPrerendered" />
MVC:
In the project's Index
view of an MVC app, add the Counter
component's namespace and embed the component into the view. When the Index
view loads, the Counter
component is prerendered in the page. In the following example, replace the {APP NAMESPACE}
placeholder with the project's namespace.
Views/Home/Index.cshtml
:
@using {APP NAMESPACE}.Views.Shared
@{
ViewData["Title"] = "Home Page";
}
<component type="typeof(Counter)" render-mode="ServerPrerendered" />
For more information, see the Render components from a page or view section.
This section pertains to adding components that are directly routable from user requests.
To support routable Razor components in Razor Pages apps:
Follow the guidance in the Configuration section.
Add an App
component to the project root with the following content.
App.razor
:
@using Microsoft.AspNetCore.Components.Routing
<Router AppAssembly="typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<p role="alert">Sorry, there's nothing at this address.</p>
</NotFound>
</Router>
Add a _Host
page to the project with the following content. Replace the {APP NAMESPACE}
placeholder with the app's namespace.
Pages/_Host.cshtml
:
@page
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<component type="typeof(App)" render-mode="ServerPrerendered" />
Note
The preceding example assumes that the HeadOutlet component and Blazor script (_framework/blazor.server.js
) are rendered by the app's layout. For more information, see the Configuration section.
RenderMode configures whether the App
component:
For more information on the Component Tag Helper, including passing parameters and RenderMode configuration, see Component Tag Helper in ASP.NET Core.
In the Program.cs
endpoints, add a low-priority route for the _Host
page as the last endpoint:
app.MapFallbackToPage("/_Host");
Add routable components to the project. The following example is a RoutableCounter
component based on the Counter
component in the Blazor project templates.
Pages/RoutableCounter.razor
:
@page "/routable-counter"
<PageTitle>Routable Counter</PageTitle>
<h1>Routable Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Run the project and navigate to the routable RoutableCounter
component at /routable-counter
.
For more information on namespaces, see the Component namespaces section.
This section pertains to adding components that are directly routable from user requests.
To support routable Razor components in MVC apps:
Follow the guidance in the Configuration section.
Add an App
component to the project root with the following content.
App.razor
:
@using Microsoft.AspNetCore.Components.Routing
<Router AppAssembly="typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<p role="alert">Sorry, there's nothing at this address.</p>
</NotFound>
</Router>
Add a _Host
view to the project with the following content. Replace the {APP NAMESPACE}
placeholder with the app's namespace.
Views/Home/_Host.cshtml
:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<component type="typeof(App)" render-mode="ServerPrerendered" />
Note
The preceding example assumes that the HeadOutlet component and Blazor script (_framework/blazor.server.js
) are rendered by the app's layout. For more information, see the Configuration section.
RenderMode configures whether the App
component:
For more information on the Component Tag Helper, including passing parameters and RenderMode configuration, see Component Tag Helper in ASP.NET Core.
Add an action to the Home controller.
Controllers/HomeController.cs
:
public IActionResult Blazor()
{
return View("_Host");
}
In the Program.cs
endpoints, add a low-priority route for the controller action that returns the _Host
view:
app.MapFallbackToController("Blazor", "Home");
Create a Pages
folder in the MVC app and add routable components. The following example is a RoutableCounter
component based on the Counter
component in the Blazor project templates.
Pages/RoutableCounter.razor
:
@page "/routable-counter"
<PageTitle>Routable Counter</PageTitle>
<h1>Routable Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Run the project and navigate to the routable RoutableCounter
component at /routable-counter
.
For more information on namespaces, see the Component namespaces section.
This section pertains to adding components to pages or views, where the components aren't directly routable from user requests.
To render a component from a page or view, use the Component Tag Helper.
Stateful interactive components can be added to a Razor page or view.
When the page or view renders:
The following Razor page renders a Counter
component:
<h1>Razor Page</h1>
<component type="typeof(Counter)" render-mode="ServerPrerendered"
param-InitialValue="InitialValue" />
@functions {
[BindProperty(SupportsGet=true)]
public int InitialValue { get; set; }
}
For more information, see Component Tag Helper in ASP.NET Core.
In the following Razor page, the Counter
component is statically rendered with an initial value that's specified using a form. Since the component is statically rendered, the component isn't interactive:
<h1>Razor Page</h1>
<form>
<input type="number" asp-for="InitialValue" />
<button type="submit">Set initial value</button>
</form>
<component type="typeof(Counter)" render-mode="Static"
param-InitialValue="InitialValue" />
@functions {
[BindProperty(SupportsGet=true)]
public int InitialValue { get; set; }
}
For more information, see Component Tag Helper in ASP.NET Core.
When using a custom folder to hold the project's Razor components, add the namespace representing the folder to either the page/view or to the _ViewImports.cshtml
file. In the following example:
Components
folder of the project.{APP NAMESPACE}
placeholder is the project's namespace. Components
represents the name of the folder.@using {APP NAMESPACE}.Components
The _ViewImports.cshtml
file is located in the Pages
folder of a Razor Pages app or the Views
folder of an MVC app.
For more information, see ASP.NET Core Razor components.
Without persisting prerendered state, state used during prerendering is lost and must be recreated when the app is fully loaded. If any state is setup asynchronously, the UI may flicker as the prerendered UI is replaced with temporary placeholders and then fully rendered again.
To persist state for prerendered components, use the Persist Component State Tag Helper (reference source). Add the Tag Helper's tag, <persist-component-state />
, inside the closing </body>
tag of the _Host
page in an app that prerenders components.
Note
Documentation links to .NET reference source usually load the repository's default branch, which represents the current development for the next release of .NET. To select a tag for a specific release, use the Switch branches or tags dropdown list. For more information, see How to select a version tag of ASP.NET Core source code (dotnet/AspNetCore.Docs #26205).
In Pages/_Host.cshtml
of Blazor apps that are ServerPrerendered
in a Blazor Server app:
<body>
...
<persist-component-state />
</body>
Decide what state to persist using the PersistentComponentState service. PersistentComponentState.RegisterOnPersisting
registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes.
In the following example:
{TYPE}
placeholder represents the type of data to persist (for example, WeatherForecast[]
).{TOKEN}
placeholder is a state identifier string (for example, fetchdata
).@implements IDisposable
@inject PersistentComponentState ApplicationState
...
@code {
private {TYPE} data;
private PersistingComponentStateSubscription persistingSubscription;
protected override async Task OnInitializedAsync()
{
persistingSubscription =
ApplicationState.RegisterOnPersisting(PersistData);
if (!ApplicationState.TryTakeFromJson<{TYPE}>(
"{TOKEN}", out var restored))
{
data = await ...;
}
else
{
data = restored!;
}
}
private Task PersistData()
{
ApplicationState.PersistAsJson("{TOKEN}", data);
return Task.CompletedTask;
}
void IDisposable.Dispose()
{
persistingSubscription.Dispose();
}
}
The following example is an updated version of the FetchData
component based on the Blazor project template. The WeatherForecastPreserveState
component persists weather forecast state during prerendering and then retrieves the state to initialize the component. The Persist Component State Tag Helper persists the component state after all component invocations.
Pages/WeatherForecastPreserveState.razor
:
@page "/weather-forecast-preserve-state"
@using BlazorSample.Shared
@implements IDisposable
@inject IWeatherForecastService WeatherForecastService
@inject PersistentComponentState ApplicationState
<PageTitle>Weather Forecast</PageTitle>
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[] forecasts = Array.Empty<WeatherForecast>();
private PersistingComponentStateSubscription persistingSubscription;
protected override async Task OnInitializedAsync()
{
persistingSubscription =
ApplicationState.RegisterOnPersisting(PersistForecasts);
if (!ApplicationState.TryTakeFromJson<WeatherForecast[]>(
"fetchdata", out var restored))
{
forecasts =
await WeatherForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
}
else
{
forecasts = restored!;
}
}
private Task PersistForecasts()
{
ApplicationState.PersistAsJson("fetchdata", forecasts);
return Task.CompletedTask;
}
void IDisposable.Dispose()
{
persistingSubscription.Dispose();
}
}
By initializing components with the same state used during prerendering, any expensive initialization steps are only executed once. The rendered UI also matches the prerendered UI, so no flicker occurs in the browser.
The persisted prerendered state is transferred to the client, where it's used to restore the component state. ASP.NET Core Data Protection ensures that the data is transferred securely in Blazor Server apps.
A large prerendered state size may exceed Blazor's SignalR circuit message size limit, which results in the following:
To resolve the problem, use either of the following approaches:
OnNavigateAsync
Prerendering can improve Search Engine Optimization (SEO) by rendering content for the initial HTTP response that search engines can use to calculate page rank.
After configuring the project, use the guidance in the following sections depending on the project's requirements:
@page
directive.
Use the following guidance to integrate Razor components into pages or views of an existing Razor Pages or MVC app.
Important
The use of a layout page (_Layout.cshtml
) with a Component Tag Helper for a HeadOutlet component is required to control <head>
content, such as the page's title (PageTitle component) and other head elements (HeadContent component). For more information, see Control head content in ASP.NET Core Blazor apps.
In the project's layout file:
Add the following <base>
tag and HeadOutlet component Tag Helper to the <head>
element in Pages/Shared/_Layout.cshtml
(Razor Pages) or Views/Shared/_Layout.cshtml
(MVC):
<base href="~/" />
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
The href
value (the app base path) in the preceding example assumes that the app resides at the root URL path (/
). If the app is a sub-application, follow the guidance in the App base path section of the Host and deploy ASP.NET Core Blazor article.
The HeadOutlet component is used to render head (<head>
) content for page titles (PageTitle component) and other head elements (HeadContent component) set by Razor components. For more information, see Control head content in ASP.NET Core Blazor apps.
Add a <script>
tag for the blazor.server.js
script immediately before the Scripts
render section (@await RenderSectionAsync(...)
) in the app's layout.
Pages/Shared/_Layout.cshtml
(Razor Pages) or Views/Shared/_Layout.cshtml
(MVC):
<script src="_framework/blazor.server.js"></script>
The framework adds the blazor.server.js
script to the app. There's no need to manually add a blazor.server.js
script file to the app.
Add an imports file to the root folder of the project with the following content. Change the {APP NAMESPACE}
placeholder to the namespace of the project.
_Imports.razor
:
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using {APP NAMESPACE}
Register the Blazor Server services in Program.cs
where services are registered:
builder.Services.AddServerSideBlazor();
Add the Blazor Hub endpoint to the endpoints of Program.cs
where routes are mapped.
Place the following line after the call to MapRazorPages
(Razor Pages) or MapControllerRoute
(MVC):
app.MapBlazorHub();
Integrate components into any page or view. For example, add a Counter
component to the project's Shared
folder.
Pages/Shared/Counter.razor
(Razor Pages) or Views/Shared/Counter.razor
(MVC):
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Razor Pages:
In the project's Index
page of a Razor Pages app, add the Counter
component's namespace and embed the component into the page. When the Index
page loads, the Counter
component is prerendered in the page. In the following example, replace the {APP NAMESPACE}
placeholder with the project's namespace.
Pages/Index.cshtml
:
@page
@using {APP NAMESPACE}.Pages.Shared
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<component type="typeof(Counter)" render-mode="ServerPrerendered" />
MVC:
In the project's Index
view of an MVC app, add the Counter
component's namespace and embed the component into the view. When the Index
view loads, the Counter
component is prerendered in the page. In the following example, replace the {APP NAMESPACE}
placeholder with the project's namespace.
Views/Home/Index.cshtml
:
@using {APP NAMESPACE}.Views.Shared
@{
ViewData["Title"] = "Home Page";
}
<component type="typeof(Counter)" render-mode="ServerPrerendered" />
For more information, see the Render components from a page or view section.
This section pertains to adding components that are directly routable from user requests.
To support routable Razor components in Razor Pages apps:
Follow the guidance in the Configuration section.
Add an App
component to the project root with the following content.
App.razor
:
@using Microsoft.AspNetCore.Components.Routing
<Router AppAssembly="typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<p role="alert">Sorry, there's nothing at this address.</p>
</NotFound>
</Router>
Add a _Host
page to the project with the following content.
Pages/_Host.cshtml
:
@page "/blazor"
@namespace {APP NAMESPACE}.Pages.Shared
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = "_Layout";
}
<component type="typeof(App)" render-mode="ServerPrerendered" />
In this scenario, components use the shared _Layout.cshtml
file for their layout.
Important
The use of a layout page (_Layout.cshtml
) with a Component Tag Helper for a HeadOutlet component is required to control <head>
content, such as the page's title (PageTitle component) and other head elements (HeadContent component). For more information, see Control head content in ASP.NET Core Blazor apps.
RenderMode configures whether the App
component:
For more information on the Component Tag Helper, including passing parameters and RenderMode configuration, see Component Tag Helper in ASP.NET Core.
In the Program.cs
endpoints, add a low-priority route for the _Host
page as the last endpoint:
app.MapFallbackToPage("/_Host");
Add routable components to the project. The following example is a RoutableCounter
component based on the Counter
component in the Blazor project templates.
Pages/RoutableCounter.razor
:
@page "/routable-counter"
<PageTitle>Routable Counter</PageTitle>
<h1>Routable Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Run the project and navigate to the routable RoutableCounter
component at /routable-counter
.
For more information on namespaces, see the Component namespaces section.
This section pertains to adding components that are directly routable from user requests.
To support routable Razor components in MVC apps:
Follow the guidance in the Configuration section.
Add an App
component to the project root with the following content.
App.razor
:
@using Microsoft.AspNetCore.Components.Routing
<Router AppAssembly="typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<p role="alert">Sorry, there's nothing at this address.</p>
</NotFound>
</Router>
Add a _Host
view to the project with the following content.
Views/Home/_Host.cshtml
:
@namespace {APP NAMESPACE}.Views.Shared
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = "_Layout";
}
<component type="typeof(App)" render-mode="ServerPrerendered" />
Components use the shared _Layout.cshtml
file for their layout.
Important
The use of a layout page (_Layout.cshtml
) with a Component Tag Helper for a HeadOutlet component is required to control <head>
content, such as the page's title (PageTitle component) and other head elements (HeadContent component). For more information, see Control head content in ASP.NET Core Blazor apps.
RenderMode configures whether the App
component:
For more information on the Component Tag Helper, including passing parameters and RenderMode configuration, see Component Tag Helper in ASP.NET Core.
Add an action to the Home controller.
Controllers/HomeController.cs
:
public IActionResult Blazor()
{
return View("_Host");
}
In the Program.cs
endpoints, add a low-priority route for the controller action that returns the _Host
view:
app.MapFallbackToController("Blazor", "Home");
Create a Pages
folder in the MVC app and add routable components. The following example is a RoutableCounter
component based on the Counter
component in the Blazor project templates.
Pages/RoutableCounter.razor
:
@page "/routable-counter"
<PageTitle>Routable Counter</PageTitle>
<h1>Routable Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Run the project and navigate to the routable RoutableCounter
component at /routable-counter
.
For more information on namespaces, see the Component namespaces section.
This section pertains to adding components to pages or views, where the components aren't directly routable from user requests.
To render a component from a page or view, use the Component Tag Helper.
Stateful interactive components can be added to a Razor page or view.
When the page or view renders:
The following Razor page renders a Counter
component:
<h1>Razor Page</h1>
<component type="typeof(Counter)" render-mode="ServerPrerendered"
param-InitialValue="InitialValue" />
@functions {
[BindProperty(SupportsGet=true)]
public int InitialValue { get; set; }
}
For more information, see Component Tag Helper in ASP.NET Core.
Important
The use of a layout page (_Layout.cshtml
) with a Component Tag Helper for a HeadOutlet component is required to control <head>
content, such as the page's title (PageTitle component) and other head elements (HeadContent component). For more information, see Control head content in ASP.NET Core Blazor apps.
In the following Razor page, the Counter
component is statically rendered with an initial value that's specified using a form. Since the component is statically rendered, the component isn't interactive:
<h1>Razor Page</h1>
<form>
<input type="number" asp-for="InitialValue" />
<button type="submit">Set initial value</button>
</form>
<component type="typeof(Counter)" render-mode="Static"
param-InitialValue="InitialValue" />
@functions {
[BindProperty(SupportsGet=true)]
public int InitialValue { get; set; }
}
For more information, see Component Tag Helper in ASP.NET Core.
Important
The use of a layout page (_Layout.cshtml
) with a Component Tag Helper for a HeadOutlet component is required to control <head>
content, such as the page's title (PageTitle component) and other head elements (HeadContent component). For more information, see Control head content in ASP.NET Core Blazor apps.
When using a custom folder to hold the project's Razor components, add the namespace representing the folder to either the page/view or to the _ViewImports.cshtml
file. In the following example:
Components
folder of the project.{APP NAMESPACE}
placeholder is the project's namespace. Components
represents the name of the folder.@using {APP NAMESPACE}.Components
The _ViewImports.cshtml
file is located in the Pages
folder of a Razor Pages app or the Views
folder of an MVC app.
For more information, see ASP.NET Core Razor components.
Without persisting prerendered state, state used during prerendering is lost and must be recreated when the app is fully loaded. If any state is setup asynchronously, the UI may flicker as the prerendered UI is replaced with temporary placeholders and then fully rendered again.
To solve these problems, Blazor supports persisting state in a prerendered page using the Persist Component State Tag Helper. Add the Tag Helper's tag, <persist-component-state />
, inside the closing </body>
tag.
Pages/_Layout.cshtml
:
<body>
...
<persist-component-state />
</body>
Decide what state to persist using the PersistentComponentState service. PersistentComponentState.RegisterOnPersisting
registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes.
The following example is an updated version of the FetchData
component based on the Blazor project template. The WeatherForecastPreserveState
component persists weather forecast state during prerendering and then retrieves the state to initialize the component. The Persist Component State Tag Helper persists the component state after all component invocations.
Pages/WeatherForecastPreserveState.razor
:
@page "/weather-forecast-preserve-state"
@implements IDisposable
@using BlazorSample.Shared
@inject IWeatherForecastService WeatherForecastService
@inject PersistentComponentState ApplicationState
<PageTitle>Weather Forecast</PageTitle>
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[] forecasts = Array.Empty<WeatherForecast>();
private PersistingComponentStateSubscription persistingSubscription;
protected override async Task OnInitializedAsync()
{
persistingSubscription =
ApplicationState.RegisterOnPersisting(PersistForecasts);
if (!ApplicationState.TryTakeFromJson<WeatherForecast[]>(
"fetchdata", out var restored))
{
forecasts =
await WeatherForecastService.GetForecastAsync(DateTime.Now);
}
else
{
forecasts = restored!;
}
}
private Task PersistForecasts()
{
ApplicationState.PersistAsJson("fetchdata", forecasts);
return Task.CompletedTask;
}
void IDisposable.Dispose()
{
persistingSubscription.Dispose();
}
}
By initializing components with the same state used during prerendering, any expensive initialization steps are only executed once. The rendered UI also matches the prerendered UI, so no flicker occurs in the browser.
The persisted prerendered state is transferred to the client, where it's used to restore the component state. ASP.NET Core Data Protection ensures that the data is transferred securely in Blazor Server apps.
A large prerendered state size may exceed Blazor's SignalR circuit message size limit, which results in the following:
To resolve the problem, use either of the following approaches:
Prerendering can improve Search Engine Optimization (SEO) by rendering content for the initial HTTP response that search engines can use to calculate page rank.
After configuring the project, use the guidance in the following sections depending on the project's requirements:
@page
directive.
An existing Razor Pages or MVC app can integrate Razor components into pages or views:
In the project's layout file:
Add the following <base>
tag to the <head>
element in Pages/Shared/_Layout.cshtml
(Razor Pages) or Views/Shared/_Layout.cshtml
(MVC):
<base href="~/" />
The href
value (the app base path) in the preceding example assumes that the app resides at the root URL path (/
). If the app is a sub-application, follow the guidance in the App base path section of the Host and deploy ASP.NET Core Blazor article.
Add a <script>
tag for the blazor.server.js
script immediately before the Scripts
render section.
Pages/Shared/_Layout.cshtml
(Razor Pages) or Views/Shared/_Layout.cshtml
(MVC):
...
<script src="_framework/blazor.server.js"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
The framework adds the blazor.server.js
script to the app. There's no need to manually add a blazor.server.js
script file to the app.
Add an imports file to the root folder of the project with the following content. Change the {APP NAMESPACE}
placeholder to the namespace of the project.
_Imports.razor
:
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@using {APP NAMESPACE}
Register the Blazor Server service in Startup.ConfigureServices
.
In Startup.cs
:
services.AddServerSideBlazor();
Add the Blazor Hub endpoint to the endpoints (app.UseEndpoints
) of Startup.Configure
.
Startup.cs
:
endpoints.MapBlazorHub();
Integrate components into any page or view. For example, add a Counter
component to the project's Shared
folder.
Pages/Shared/Counter.razor
(Razor Pages) or Views/Shared/Counter.razor
(MVC):
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Razor Pages:
In the project's Index
page of a Razor Pages app, add the Counter
component's namespace and embed the component into the page. When the Index
page loads, the Counter
component is prerendered in the page. In the following example, replace the {APP NAMESPACE}
placeholder with the project's namespace.
Pages/Index.cshtml
:
@page
@using {APP NAMESPACE}.Pages.Shared
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div>
<component type="typeof(Counter)" render-mode="ServerPrerendered" />
</div>
In the preceding example, replace the {APP NAMESPACE}
placeholder with the app's namespace.
MVC:
In the project's Index
view of an MVC app, add the Counter
component's namespace and embed the component into the view. When the Index
view loads, the Counter
component is prerendered in the page. In the following example, replace the {APP NAMESPACE}
placeholder with the project's namespace.
Views/Home/Index.cshtml
:
@using {APP NAMESPACE}.Views.Shared
@{
ViewData["Title"] = "Home Page";
}
<div>
<component type="typeof(Counter)" render-mode="ServerPrerendered" />
</div>
For more information, see the Render components from a page or view section.
This section pertains to adding components that are directly routable from user requests.
To support routable Razor components in Razor Pages apps:
Follow the guidance in the Configuration section.
Add an App
component to the project root with the following content.
App.razor
:
@using Microsoft.AspNetCore.Components.Routing
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" />
</Found>
<NotFound>
<h1>Page not found</h1>
<p>Sorry, but there's nothing here!</p>
</NotFound>
</Router>
Note
With the release of ASP.NET Core 5.0.1 and for any additional 5.x releases, the Router
component includes the PreferExactMatches
parameter set to @true
. For more information, see Migrate from ASP.NET Core 3.1 to 5.0.
Add a _Host
page to the project with the following content.
Pages/_Host.cshtml
:
@page "/blazor"
@{
Layout = "_Layout";
}
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
Components use the shared _Layout.cshtml
file for their layout.
RenderMode configures whether the App
component:
For more information on the Component Tag Helper, including passing parameters and RenderMode configuration, see Component Tag Helper in ASP.NET Core.
In the Startup.Configure
endpoints of Startup.cs
, add a low-priority route for the _Host
page as the last endpoint:
endpoints.MapFallbackToPage("/_Host");
The following example shows the added line in a typical app's endpoint configuration:
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
Add routable components to the project.
Pages/RoutableCounter.razor
:
@page "/routable-counter"
<h1>Routable Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Run the project and navigate to the routable RoutableCounter
component at /routable-counter
.
For more information on namespaces, see the Component namespaces section.
This section pertains to adding components that are directly routable from user requests.
To support routable Razor components in MVC apps:
Follow the guidance in the Configuration section.
Add an App
component to the project root with the following content.
App.razor
:
@using Microsoft.AspNetCore.Components.Routing
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" />
</Found>
<NotFound>
<h1>Page not found</h1>
<p>Sorry, but there's nothing here!</p>
</NotFound>
</Router>
Note
With the release of ASP.NET Core 5.0.1 and for any additional 5.x releases, the Router
component includes the PreferExactMatches
parameter set to @true
. For more information, see Migrate from ASP.NET Core 3.1 to 5.0.
Add a _Host
view to the project with the following content.
Views/Home/_Host.cshtml
:
@{
Layout = "_Layout";
}
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
Components use the shared _Layout.cshtml
file for their layout.
RenderMode configures whether the App
component:
For more information on the Component Tag Helper, including passing parameters and RenderMode configuration, see Component Tag Helper in ASP.NET Core.
Add an action to the Home controller.
Controllers/HomeController.cs
:
public IActionResult Blazor()
{
return View("_Host");
}
In the Startup.Configure
endpoints of Startup.cs
, add a low-priority route for the controller action that returns the _Host
view:
endpoints.MapFallbackToController("Blazor", "Home");
The following example shows the added line in a typical app's endpoint configuration:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapBlazorHub();
endpoints.MapFallbackToController("Blazor", "Home");
});
Add routable components to the project.
Pages/RoutableCounter.razor
:
@page "/routable-counter"
<h1>Routable Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Run the project and navigate to the routable RoutableCounter
component at /routable-counter
.
For more information on namespaces, see the Component namespaces section.
This section pertains to adding components to pages or views, where the components aren't directly routable from user requests.
To render a component from a page or view, use the Component Tag Helper.
Stateful interactive components can be added to a Razor page or view.
When the page or view renders:
The following Razor page renders a Counter
component:
<h1>My Razor Page</h1>
<component type="typeof(Counter)" render-mode="ServerPrerendered"
param-InitialValue="InitialValue" />
@functions {
[BindProperty(SupportsGet=true)]
public int InitialValue { get; set; }
}
For more information, see Component Tag Helper in ASP.NET Core.
In the following Razor page, the Counter
component is statically rendered with an initial value that's specified using a form. Since the component is statically rendered, the component isn't interactive:
<h1>My Razor Page</h1>
<form>
<input type="number" asp-for="InitialValue" />
<button type="submit">Set initial value</button>
</form>
<component type="typeof(Counter)" render-mode="Static"
param-InitialValue="InitialValue" />
@functions {
[BindProperty(SupportsGet=true)]
public int InitialValue { get; set; }
}
For more information, see Component Tag Helper in ASP.NET Core.
When using a custom folder to hold the project's Razor components, add the namespace representing the folder to either the page/view or to the _ViewImports.cshtml
file. In the following example:
Components
folder of the project.{APP NAMESPACE}
placeholder is the project's namespace. Components
represents the name of the folder.@using {APP NAMESPACE}.Components
The _ViewImports.cshtml
file is located in the Pages
folder of a Razor Pages app or the Views
folder of an MVC app.
For more information, see ASP.NET Core Razor components.
A large prerendered state size may exceed Blazor's SignalR circuit message size limit, which results in the following:
To resolve the problem, use either of the following approaches:
Razor components can be integrated into Razor Pages or MVC apps. When the page or view is rendered, components can be prerendered at the same time.
Prerendering can improve Search Engine Optimization (SEO) by rendering content for the initial HTTP response that search engines can use to calculate page rank.
After configuring the project, use the guidance in the following sections depending on the project's requirements:
@page
directive.
An existing Razor Pages or MVC app can integrate Razor components into pages or views:
In the project's layout file:
Add the following <base>
tag to the <head>
element in Pages/Shared/_Layout.cshtml
(Razor Pages) or Views/Shared/_Layout.cshtml
(MVC):
+ <base href="~/" />
The href
value (the app base path) in the preceding example assumes that the app resides at the root URL path (/
). If the app is a sub-application, follow the guidance in the App base path section of the Host and deploy ASP.NET Core Blazor article.
Add a <script>
tag for the blazor.server.js
script immediately before the Scripts
render section.
Pages/Shared/_Layout.cshtml
(Razor Pages) or Views/Shared/_Layout.cshtml
(MVC):
...
<script src="_framework/blazor.server.js"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
The framework adds the blazor.server.js
script to the app. There's no need to manually add a blazor.server.js
script file to the app.
Add an imports file to the root folder of the project with the following content. Change the {APP NAMESPACE}
placeholder to the namespace of the project.
_Imports.razor
:
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@using {APP NAMESPACE}
Register the Blazor Server service in Startup.ConfigureServices
.
Startup.cs
:
services.AddServerSideBlazor();
Add the Blazor Hub endpoint to the endpoints (app.UseEndpoints
) of Startup.Configure
.
Startup.cs
:
endpoints.MapBlazorHub();
Integrate components into any page or view. For example, add a Counter
component to the project's Shared
folder.
Pages/Shared/Counter.razor
(Razor Pages) or Views/Shared/Counter.razor
(MVC):
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Razor Pages:
In the project's Index
page of a Razor Pages app, add the Counter
component's namespace and embed the component into the page. When the Index
page loads, the Counter
component is prerendered in the page. In the following example, replace the {APP NAMESPACE}
placeholder with the project's namespace.
Pages/Index.cshtml
:
@page
@using {APP NAMESPACE}.Pages.Shared
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div>
<component type="typeof(Counter)" render-mode="ServerPrerendered" />
</div>
In the preceding example, replace the {APP NAMESPACE}
placeholder with the app's namespace.
MVC:
In the project's Index
view of an MVC app, add the Counter
component's namespace and embed the component into the view. When the Index
view loads, the Counter
component is prerendered in the page. In the following example, replace the {APP NAMESPACE}
placeholder with the project's namespace.
Views/Home/Index.cshtml
:
@using {APP NAMESPACE}.Views.Shared
@{
ViewData["Title"] = "Home Page";
}
<div>
<component type="typeof(Counter)" render-mode="ServerPrerendered" />
</div>
For more information, see the Render components from a page or view section.
This section pertains to adding components that are directly routable from user requests.
To support routable Razor components in Razor Pages apps:
Follow the guidance in the Configuration section.
Add an App
component to the project root with the following content.
App.razor
:
@using Microsoft.AspNetCore.Components.Routing
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" />
</Found>
<NotFound>
<h1>Page not found</h1>
<p>Sorry, but there's nothing here!</p>
</NotFound>
</Router>
Add a _Host
page to the project with the following content.
Pages/_Host.cshtml
:
@page "/blazor"
@{
Layout = "_Layout";
}
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
Components use the shared _Layout.cshtml
file for their layout.
RenderMode configures whether the App
component:
For more information on the Component Tag Helper, including passing parameters and RenderMode configuration, see Component Tag Helper in ASP.NET Core.
In the Startup.Configure
endpoints of Startup.cs
, add a low-priority route for the _Host
page as the last endpoint:
endpoints.MapFallbackToPage("/_Host");
The following example shows the added line in a typical app's endpoint configuration:
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
Add routable components to the project.
Pages/RoutableCounter.razor
:
@page "/routable-counter"
<h1>Routable Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Run the project and navigate to the routable RoutableCounter
component at /routable-counter
.
For more information on namespaces, see the Component namespaces section.
This section pertains to adding components that are directly routable from user requests.
To support routable Razor components in MVC apps:
Follow the guidance in the Configuration section.
Add an App
component to the project root with the following content.
App.razor
:
@using Microsoft.AspNetCore.Components.Routing
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" />
</Found>
<NotFound>
<h1>Page not found</h1>
<p>Sorry, but there's nothing here!</p>
</NotFound>
</Router>
Add a _Host
view to the project with the following content.
Views/Home/_Host.cshtml
:
@{
Layout = "_Layout";
}
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
Components use the shared _Layout.cshtml
file for their layout.
RenderMode configures whether the App
component:
For more information on the Component Tag Helper, including passing parameters and RenderMode configuration, see Component Tag Helper in ASP.NET Core.
Add an action to the Home controller.
Controllers/HomeController.cs
:
public IActionResult Blazor()
{
return View("_Host");
}
In the Startup.Configure
endpoints of Startup.cs
, add a low-priority route for the controller action that returns the _Host
view:
endpoints.MapFallbackToController("Blazor", "Home");
The following example shows the added line in a typical app's endpoint configuration:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapBlazorHub();
endpoints.MapFallbackToController("Blazor", "Home");
});
Add routable components to the project.
Pages/RoutableCounter.razor
:
@page "/routable-counter"
<h1>Routable Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Run the project and navigate to the routable RoutableCounter
component at /routable-counter
.
For more information on namespaces, see the Component namespaces section.
This section pertains to adding components to pages or views, where the components aren't directly routable from user requests.
To render a component from a page or view, use the Component Tag Helper.
Stateful interactive components can be added to a Razor page or view.
When the page or view renders:
The following Razor page renders a Counter
component:
<h1>My Razor Page</h1>
<component type="typeof(Counter)" render-mode="ServerPrerendered"
param-InitialValue="InitialValue" />
@functions {
[BindProperty(SupportsGet=true)]
public int InitialValue { get; set; }
}
For more information, see Component Tag Helper in ASP.NET Core.
In the following Razor page, the Counter
component is statically rendered with an initial value that's specified using a form. Since the component is statically rendered, the component isn't interactive:
<h1>My Razor Page</h1>
<form>
<input type="number" asp-for="InitialValue" />
<button type="submit">Set initial value</button>
</form>
<component type="typeof(Counter)" render-mode="Static"
param-InitialValue="InitialValue" />
@functions {
[BindProperty(SupportsGet=true)]
public int InitialValue { get; set; }
}
For more information, see Component Tag Helper in ASP.NET Core.
When using a custom folder to hold the project's Razor components, add the namespace representing the folder to either the page/view or to the _ViewImports.cshtml
file. In the following example:
Components
folder of the project.{APP NAMESPACE}
placeholder is the project's namespace. Components
represents the name of the folder.@using {APP NAMESPACE}.Components
The _ViewImports.cshtml
file is located in the Pages
folder of a Razor Pages app or the Views
folder of an MVC app.
For more information, see ASP.NET Core Razor components.
A large prerendered state size may exceed Blazor's SignalR circuit message size limit, which results in the following:
To resolve the problem, use either of the following approaches:
ASP.NET Core feedback
ASP.NET Core is an open source project. Select a link to provide feedback:
Events
Mar 31, 11 PM - Apr 2, 11 PM
The ultimate Microsoft Fabric, Power BI, SQL, and AI community-led event. March 31 to April 2, 2025.
Register todayTraining
Module
Use pages, routing, and layouts to improve Blazor navigation - Training
Learn how to optimize your app's navigation, use parameters from the URL, and create reusable layouts in a Blazor web app.
Documentation
Learn how to create and use Razor components in Blazor apps, including guidance on Razor syntax, component naming, namespaces, and component parameters.
Use Razor components in JavaScript apps and SPA frameworks
Learn how to create and use Razor components in JavaScript apps and SPA frameworks.
ASP.NET Core Blazor render modes
Learn about Blazor render modes and how to apply them in Blazor Web Apps.