--- title: Integrate ASP.NET Core Razor components with MVC or Razor Pages in hosted Blazor WebAssembly solutions author: guardrex description: Learn about Razor component integration scenarios for hosted Blazor WebAssembly apps with MVC or Razor Pages, including prerendering of Razor components on the server. monikerRange: '>= aspnetcore-3.1 < aspnetcore-8.0' ms.author: riande ms.custom: mvc ms.date: 11/12/2024 uid: blazor/components/integration-hosted-webassembly --- # Integrate ASP.NET Core Razor components with MVC or Razor Pages in hosted Blazor WebAssembly solutions > [!NOTE] > Hosted Blazor WebAssembly solutions remain supported, but the project template was removed and is no longer supported in .NET 8 or later. This article appears in the table of contents up to .NET 7 for reference, but note that .NET 7 is a [Standard Support Term](https://dotnet.microsoft.com/platform/support/policy/dotnet-core) release that's no longer supported. :::moniker range="= aspnetcore-7.0 || = aspnetcore-5.0 || = aspnetcore-3.0 || = aspnetcore-3.1 || = aspnetcore-2.0" > [!WARNING] > This version of ASP.NET Core is no longer supported. For more information, see the [.NET and .NET Core Support Policy](https://dotnet.microsoft.com/platform/support/policy/dotnet-core). :::moniker-end This article explains Razor component integration scenarios for hosted Blazor WebAssembly apps, including prerendering of Razor components on the server. > [!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. :::moniker range=">= aspnetcore-7.0" Prerendering can improve [Search Engine Optimization (SEO)](https://developer.mozilla.org/docs/Glossary/SEO) by rendering content for the initial HTTP response that search engines can use to calculate page rank. ## Solution configuration ### Prerendering configuration To set up prerendering for a hosted Blazor WebAssembly app: 1. Host the Blazor WebAssembly app in an ASP.NET Core app. A standalone Blazor WebAssembly app can be added to an ASP.NET Core solution, or you can use a hosted Blazor WebAssembly app created from the [Blazor WebAssembly project template](xref:blazor/tooling) with the hosted option: * Visual Studio: In the **Additional information** dialog, select the **ASP.NET Core Hosted** checkbox when creating the Blazor WebAssembly app. In this article's examples, the solution is named `BlazorHosted`. * Visual Studio Code/.NET CLI command shell: `dotnet new blazorwasm -ho` (use the `-ho|--hosted` option). Use the `-o|--output {LOCATION}` option to create a folder for the solution and set the solution's project namespaces. In this article's examples, the solution is named `BlazorHosted` (`dotnet new blazorwasm -ho -o BlazorHosted`). For the examples in this article, the hosted solution's name (assembly name) is `BlazorHosted`. The client project's namespace is `BlazorHosted.Client`, and the server project's namespace is `BlazorHosted.Server`. 1. **Delete** the `wwwroot/index.html` file from the Blazor WebAssembly **:::no-loc text="Client":::** project. 1. In the **:::no-loc text="Client":::** project, **delete** the following lines in `Program.cs`: ```diff - builder.RootComponents.Add("#app"); - builder.RootComponents.Add("head::after"); ``` 1. Add `_Host.cshtml` file to the **:::no-loc text="Server":::** project's `Pages` folder. You can obtain the files from a project created from the Blazor Server template using Visual Studio or using the .NET CLI with the `dotnet new blazorserver -o BlazorServer` command in a command shell (the `-o BlazorServer` option creates a folder for the project). After placing the files into the **:::no-loc text="Server":::** project's `Pages` folder, make the following changes to the files. Make the following changes to the `_Host.cshtml` file: * Update the `Pages` namespace at the top of the file to match the namespace of the **:::no-loc text="Server":::** app's pages. The `{APP NAMESPACE}` placeholder in the following example represents the namespace of the donor app's pages that provided the `_Host.cshtml` file: Delete: ```diff - @namespace {APP NAMESPACE}.Pages ``` Add: ```razor @namespace BlazorHosted.Server.Pages ``` * Add an [`@using`](xref:mvc/views/razor#using) directive for the **:::no-loc text="Client":::** project at the top of the file: ```razor @using BlazorHosted.Client ``` * Update the stylesheet links to point to the WebAssembly project's stylesheets. In the following example, the client project's namespace is `BlazorHosted.Client`. The `{APP NAMESPACE}` placeholder represents the namespace of the donor app that provided the `_Host.cshtml` file. Update the Component Tag Helper (`` tag) for the `HeadOutlet` component to prerender the component. Delete: ```diff - - - ``` Add: ```cshtml ``` > [!NOTE] > Leave the `` element that requests the Bootstrap stylesheet (`css/bootstrap/bootstrap.min.css`) in place. * Update the Blazor script source to use the client-side Blazor WebAssembly script: Delete: ```diff - ``` Add: ```html ``` * Update the `render-mode` of the [Component Tag Helper](xref:mvc/views/tag-helpers/builtin-th/component-tag-helper) to prerender the root `App` component with : Delete: ```diff - ``` Add: ```cshtml ``` > [!IMPORTANT] > Prerendering isn't supported for authentication endpoints (`/authentication/` path segment). For more information, see . 1. In the `Program.cs` file of the **:::no-loc text="Server":::** project, change the fallback endpoint from the `index.html` file to the `_Host.cshtml` page: Delete: ```diff - app.MapFallbackToFile("index.html"); ``` Add: ```csharp app.MapFallbackToPage("/_Host"); ``` 1. If the **:::no-loc text="Client":::** and **:::no-loc text="Server":::** projects use one or more common services during prerendering, factor the service registrations into a method that can be called from both projects. For more information, see . 1. Run the **:::no-loc text="Server":::** project. The hosted Blazor WebAssembly app is prerendered by the **:::no-loc text="Server":::** project for clients. ### Configuration for embedding Razor components into pages or views The following sections and examples for embedding Razor components from the **:::no-loc text="Client":::** Blazor WebAssembly app into pages or views of the server app require additional configuration. The **:::no-loc text="Server":::** project must have the following files and folders. Razor Pages: * `Pages/Shared/_Layout.cshtml` * `Pages/Shared/_Layout.cshtml.css` * `Pages/_ViewImports.cshtml` * `Pages/_ViewStart.cshtml` MVC: * `Views/Shared/_Layout.cshtml` * `Views/Shared/_Layout.cshtml.css` * `Views/_ViewImports.cshtml` * `Views/_ViewStart.cshtml` The preceding files can be obtained by generating an app from the ASP.NET Core project templates using: * Visual Studio's new project creation tools. * Opening a command shell and executing `dotnet new webapp -o {PROJECT NAME}` (Razor Pages) or `dotnet new mvc -o {PROJECT NAME}` (MVC). The option `-o|--output` with a value for the `{PROJECT NAME}` placeholder provides a name for the app and creates a folder for the app. Update the namespaces in the imported `_ViewImports.cshtml` file to match those in use by the **:::no-loc text="Server":::** project receiving the files. `Pages/_ViewImports.cshtml` (Razor Pages): ```razor @using BlazorHosted.Server @namespace BlazorHosted.Server.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers ``` `Views/_ViewImports.cshtml` (MVC): ```razor @using BlazorHosted.Server @using BlazorHosted.Server.Models @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers ``` Update the imported layout file, which is `Pages/Shared/_Layout.cshtml` for Razor Pages or `Views/Shared/_Layout.cshtml` for MVC. First, delete the title and the stylesheet from the donor project, which is `RPDonor.styles.css` in the following example. The `{PROJECT NAME}` placeholder represents the donor project's app name. ```diff - @ViewData["Title"] - {PROJECT NAME} - ``` Include the **:::no-loc text="Client":::** project's styles in the layout file. In the following example, the **:::no-loc text="Client":::** project's namespace is `BlazorHosted.Client`. The `` element can be updated at the same time. Place the following lines in the `<head>` content of the layout file: ```cshtml <title>@ViewData["Title"] - BlazorHosted ``` The imported layout contains two `Home` (`Index` page) and `Privacy` navigation links. To make the `Home` links point to the hosted Blazor WebAssembly app, change the hyperlinks: ```diff - {PROJECT NAME} + BlazorHosted ``` ```diff - Home + Home ``` In an MVC layout file: ```diff - {PROJECT NAME} + BlazorHosted ``` ```diff - Home + Home ``` Update the `