AspNetCore.Docs/aspnetcore/blazor/project-structure.md

525 lines
45 KiB
Markdown
Raw Normal View History

2021-08-09 03:43:46 +08:00
---
title: ASP.NET Core Blazor project structure
author: guardrex
description: Learn about ASP.NET Core Blazor app project structure.
monikerRange: '>= aspnetcore-3.1'
ms.author: riande
ms.custom: mvc
2022-11-09 00:43:50 +08:00
ms.date: 11/08/2022
2021-08-09 03:43:46 +08:00
uid: blazor/project-structure
---
# ASP.NET Core Blazor project structure
[!INCLUDE[](~/includes/not-latest-version.md)]
2023-04-04 23:06:06 +08:00
2022-03-15 17:53:00 +08:00
This article describes the files and folders that make up a Blazor app generated from a Blazor project template.
2021-08-09 03:43:46 +08:00
:::moniker range=">= aspnetcore-8.0"
## Blazor Web App
Blazor Web App project template: `blazor`
Project structure:
* `Models` folder: Contains the weather data `WeatherForecast` class (`WeatherForecast.cs`) for the app's `ShowData` component.
* `Pages` folder: Contains the Blazor app's routable Razor components (`.razor`). The route for each page is specified using the [`@page`](xref:mvc/views/razor#page) directive. The template includes the following:
* `Counter` component (`Counter.razor`): Implements the *Counter* page.
* `ShowData` component (`ShowData.razor`): Implements the *Weather forecast* page.
* `Index` component (`Index.razor`): Implements the *Home* page.
* `Properties` folder: Holds [development environment configuration](xref:fundamentals/environments#development-and-launchsettingsjson) in the `launchSettings.json` file.
* `Shared` folder: Contains the following shared components and stylesheets:
* `MainLayout` component (`MainLayout.razor`): The app's [layout component](xref:blazor/components/layouts).
* `MainLayout.razor.css`: Stylesheet for the app's main layout.
* `NavMenu` component (`NavMenu.razor`): Implements sidebar navigation. Includes the [`NavLink` component](xref:blazor/fundamentals/routing#navlink-and-navmenu-components) (<xref:Microsoft.AspNetCore.Components.Routing.NavLink>), which renders navigation links to other Razor components. The <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component indicates to the user which component is currently displayed.
* `NavMenu.razor.css`: Stylesheet for the app's navigation menu.
* `wwwroot` folder: The [Web Root](xref:fundamentals/index#web-root) folder for the app containing the app's public static assets.
* `_Imports.razor`: Includes common Razor directives to include in the app's components (`.razor`), such as [`@using`](xref:mvc/views/razor#using) directives for namespaces.
* `App.razor`: The root component of the app that sets up routing using the <xref:Microsoft.AspNetCore.Components.Routing.Router> component. For client-side interactive components, the <xref:Microsoft.AspNetCore.Components.Routing.Router> component intercepts browser navigation and renders the page that matches the requested address.
* `appsettings.json` and environmental app settings files: Provide [configuration settings](xref:blazor/fundamentals/configuration) for the app.
2023-06-30 19:45:44 +08:00
* `Program.cs`/`Program.Server.cs`/`Program.Browser.cs`: The app's entry point that sets up the ASP.NET Core web application or WebAssembly [host](xref:fundamentals/host/generic-host#host-definition) and contains the app's startup logic, including service registrations, configuration, logging, and request processing pipeline.
If a Blazor Web App app is only created for Blazor Server hosting, a single file named `Program.cs` is created by the Blazor project template. If a Blazor Web App is created for both Blazor Server and Blazor WebAssembly hosting, two files are created:
* `Program.Server.cs` is created for server-side execution by the server-side .NET runtime.
* `Program.Browser.cs` is created for client-side execution by the client-side .NET runtime based on WebAssembly.
A `Program` file:
* Specifies the app's [dependency injection (DI)](xref:fundamentals/dependency-injection) services. Services for Razor components are added by calling `AddRazorComponents`. For Blazor Server hosting, `AddServerComponents` adds services to support rendering interactive server components, and `AddWebAssemblyComponents` adds services to support rendering interactive WebAssembly components.
* Configures the app's request handling pipeline. For Blazor Server hosting, `MapRazorComponents` discovers available components and specifies the root component for the app, which by default is the `App` component (`App.razor`).
Additional files and folders may appear in an app produced from a Blazor Web App project template when additional options are configured. For example, generating an app with ASP.NET Core Identity includes additional assets for authentication and authorization features.
:::moniker-end
:::moniker range="< aspnetcore-8.0"
2021-08-09 03:43:46 +08:00
## Blazor Server
:::moniker-end
:::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0"
2022-11-09 00:43:50 +08:00
Blazor Server project templates: `blazorserver`, `blazorserver-empty`
2021-08-09 03:43:46 +08:00
2022-11-09 00:43:50 +08:00
The Blazor Server templates create the initial files and directory structure for a Blazor Server app:
* If the `blazorserver` template is used, the app is populated with the following:
* Demonstration code for a `FetchData` component that loads data from a weather forecast service (`WeatherForecastService`) and user interaction with a `Counter` component.
* [Bootstrap](https://getbootstrap.com/) frontend toolkit.
* If the `blazorserver-empty` template is used, the app is created without demonstration code and Bootstrap.
Project structure:
2021-08-09 03:43:46 +08:00
* `Data` folder: Contains the `WeatherForecast` class and implementation of the `WeatherForecastService` that provides example weather data to the app's `FetchData` component.
* `Pages` folder: Contains the Blazor app's routable Razor components (`.razor`) and the root Razor page of a Blazor Server app. The route for each page is specified using the [`@page`](xref:mvc/views/razor#page) directive. The template includes the following:
2021-08-09 03:43:46 +08:00
* `_Host.cshtml`: The root page of the app implemented as a Razor Page:
* When any page of the app is initially requested, this page is rendered and returned in the response.
* The Host page specifies where the root `App` component (`App.razor`) is rendered.
* `Counter` component (`Counter.razor`): Implements the Counter page.
* `Error` component (`Error.razor`): Rendered when an unhandled exception occurs in the app.
* `FetchData` component (`FetchData.razor`): Implements the Fetch data page.
* `Index` component (`Index.razor`): Implements the Home page.
* `Properties` folder: Holds [development environment configuration](xref:fundamentals/environments#development-and-launchsettingsjson) in the `launchSettings.json` file.
2021-08-09 03:43:46 +08:00
* `Shared` folder: Contains the following shared components and stylesheets:
* `MainLayout` component (`MainLayout.razor`): The app's [layout component](xref:blazor/components/layouts).
* `MainLayout.razor.css`: Stylesheet for the app's main layout.
* `NavMenu` component (`NavMenu.razor`): Implements sidebar navigation. Includes the [`NavLink` component](xref:blazor/fundamentals/routing#navlink-and-navmenu-components) (<xref:Microsoft.AspNetCore.Components.Routing.NavLink>), which renders navigation links to other Razor components. The <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.
* `NavMenu.razor.css`: Stylesheet for the app's navigation menu.
* `SurveyPrompt` component (`SurveyPrompt.razor`): Blazor survey component.
* `wwwroot` folder: The [Web Root](xref:fundamentals/index#web-root) folder for the app containing the app's public static assets.
2021-08-09 03:43:46 +08:00
* `_Imports.razor`: Includes common Razor directives to include in the app's components (`.razor`), such as [`@using`](xref:mvc/views/razor#using) directives for namespaces.
* `App.razor`: The root component of the app that sets up client-side routing using the <xref:Microsoft.AspNetCore.Components.Routing.Router> component. The <xref:Microsoft.AspNetCore.Components.Routing.Router> component intercepts browser navigation and renders the page that matches the requested address.
* `appsettings.json` and environmental app settings files: Provide [configuration settings](xref:blazor/fundamentals/configuration) for the app.
2021-10-07 21:20:17 +08:00
* `Program.cs`: The app's entry point that sets up the ASP.NET Core [host](xref:fundamentals/host/generic-host) and contains the app's startup logic, including service registrations and request processing pipeline configuration:
2021-08-09 03:43:46 +08:00
2021-10-07 21:20:17 +08:00
* Specifies the app's [dependency injection (DI)](xref:fundamentals/dependency-injection) services. Services are added by calling <xref:Microsoft.Extensions.DependencyInjection.ComponentServiceCollectionExtensions.AddServerSideBlazor%2A>, and the `WeatherForecastService` is added to the service container for use by the example `FetchData` component.
* Configures the app's request handling pipeline:
2021-08-09 03:43:46 +08:00
* <xref:Microsoft.AspNetCore.Builder.ComponentEndpointRouteBuilderExtensions.MapBlazorHub%2A> is called to set up an endpoint for the real-time connection with the browser. The connection is created with [SignalR](xref:signalr/introduction), which is a framework for adding real-time web functionality to apps.
* [`MapFallbackToPage("/_Host")`](xref:Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.MapFallbackToPage%2A) is called to set up the root page of the app (`Pages/_Host.cshtml`) and enable navigation.
Additional files and folders may appear in an app produced from a Blazor Server project template when additional options are configured. For example, generating an app with ASP.NET Core Identity includes additional assets for authentication and authorization features.
:::moniker-end
2021-08-09 03:43:46 +08:00
:::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0"
2021-08-09 03:43:46 +08:00
Blazor Server project template: `blazorserver`
2022-11-09 00:43:50 +08:00
The Blazor Server template creates the initial files and directory structure for a Blazor Server app. The app is populated with demonstration code for a `FetchData` component that loads data from a registered service, `WeatherForecastService`, and user interaction with a `Counter` component.
2022-11-09 00:43:50 +08:00
* `Data` folder: Contains the `WeatherForecast` class and implementation of the `WeatherForecastService` that provides example weather data to the app's `FetchData` component.
2021-08-09 03:43:46 +08:00
* `Pages` folder: Contains the Blazor app's routable Razor components (`.razor`) and the root Razor page of a Blazor Server app. The route for each page is specified using the [`@page`](xref:mvc/views/razor#page) directive. The template includes the following:
* `_Host.cshtml`: The root page of the app implemented as a Razor Page:
* When any page of the app is initially requested, this page is rendered and returned in the response.
* The Host page specifies where the root `App` component (`App.razor`) is rendered.
* `_Layout.cshtml`: The layout page for the `_Host.cshtml` root page of the app.
2021-08-09 03:43:46 +08:00
* `Counter` component (`Counter.razor`): Implements the Counter page.
* `Error` component (`Error.razor`): Rendered when an unhandled exception occurs in the app.
2021-08-09 03:43:46 +08:00
* `FetchData` component (`FetchData.razor`): Implements the Fetch data page.
* `Index` component (`Index.razor`): Implements the Home page.
* `Properties` folder: Holds [development environment configuration](xref:fundamentals/environments#development-and-launchsettingsjson) in the `launchSettings.json` file.
2021-08-09 03:43:46 +08:00
* `Shared` folder: Contains the following shared components and stylesheets:
* `MainLayout` component (`MainLayout.razor`): The app's [layout component](xref:blazor/components/layouts).
* `MainLayout.razor.css`: Stylesheet for the app's main layout.
* `NavMenu` component (`NavMenu.razor`): Implements sidebar navigation. Includes the [`NavLink` component](xref:blazor/fundamentals/routing#navlink-and-navmenu-components) (<xref:Microsoft.AspNetCore.Components.Routing.NavLink>), which renders navigation links to other Razor components. The <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.
* `NavMenu.razor.css`: Stylesheet for the app's navigation menu.
* `SurveyPrompt` component (`SurveyPrompt.razor`): Blazor survey component.
* `wwwroot` folder: The [Web Root](xref:fundamentals/index#web-root) folder for the app containing the app's public static assets.
2021-08-09 03:43:46 +08:00
* `_Imports.razor`: Includes common Razor directives to include in the app's components (`.razor`), such as [`@using`](xref:mvc/views/razor#using) directives for namespaces.
* `App.razor`: The root component of the app that sets up client-side routing using the <xref:Microsoft.AspNetCore.Components.Routing.Router> component. The <xref:Microsoft.AspNetCore.Components.Routing.Router> component intercepts browser navigation and renders the page that matches the requested address.
* `appsettings.json` and environmental app settings files: Provide [configuration settings](xref:blazor/fundamentals/configuration) for the app.
2021-08-09 03:43:46 +08:00
* `Program.cs`: The app's entry point that sets up the ASP.NET Core [host](xref:fundamentals/host/generic-host) and contains the app's startup logic, including service registrations and request processing pipeline configuration:
* Specifies the app's [dependency injection (DI)](xref:fundamentals/dependency-injection) services. Services are added by calling <xref:Microsoft.Extensions.DependencyInjection.ComponentServiceCollectionExtensions.AddServerSideBlazor%2A>, and the `WeatherForecastService` is added to the service container for use by the example `FetchData` component.
* Configures the app's request handling pipeline:
* <xref:Microsoft.AspNetCore.Builder.ComponentEndpointRouteBuilderExtensions.MapBlazorHub%2A> is called to set up an endpoint for the real-time connection with the browser. The connection is created with [SignalR](xref:signalr/introduction), which is a framework for adding real-time web functionality to apps.
* [`MapFallbackToPage("/_Host")`](xref:Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.MapFallbackToPage%2A) is called to set up the root page of the app (`Pages/_Host.cshtml`) and enable navigation.
2023-01-11 23:30:53 +08:00
Additional files and folders may appear in an app produced from a Blazor Server project template when additional options are configured. For example, generating an app with ASP.NET Core Identity includes additional assets for authentication and authorization features.
2023-01-11 23:30:53 +08:00
:::moniker-end
2023-01-11 23:30:53 +08:00
:::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0"
2023-01-11 23:30:53 +08:00
Blazor Server project template: `blazorserver`
2023-01-11 23:30:53 +08:00
The Blazor Server template creates the initial files and directory structure for a Blazor Server app. The app is populated with demonstration code for a `FetchData` component that loads data from a registered service, `WeatherForecastService`, and user interaction with a `Counter` component.
* `Data` folder: Contains the `WeatherForecast` class and implementation of the `WeatherForecastService` that provides example weather data to the app's `FetchData` component.
* `Pages` folder: Contains the Blazor app's routable Razor components (`.razor`) and the root Razor page of a Blazor Server app. The route for each page is specified using the [`@page`](xref:mvc/views/razor#page) directive. The template includes the following:
* `_Host.cshtml`: The root page of the app implemented as a Razor Page:
* When any page of the app is initially requested, this page is rendered and returned in the response.
* The Host page specifies where the root `App` component (`App.razor`) is rendered.
* `Counter` component (`Counter.razor`): Implements the Counter page.
* `Error` component (`Error.razor`): Rendered when an unhandled exception occurs in the app.
* `FetchData` component (`FetchData.razor`): Implements the Fetch data page.
* `Index` component (`Index.razor`): Implements the Home page.
* `Properties` folder: Holds [development environment configuration](xref:fundamentals/environments#development-and-launchsettingsjson) in the `launchSettings.json` file.
* `Shared` folder: Contains the following shared components and stylesheets:
* `MainLayout` component (`MainLayout.razor`): The app's [layout component](xref:blazor/components/layouts).
* `MainLayout.razor.css`: Stylesheet for the app's main layout.
* `NavMenu` component (`NavMenu.razor`): Implements sidebar navigation. Includes the [`NavLink` component](xref:blazor/fundamentals/routing#navlink-and-navmenu-components) (<xref:Microsoft.AspNetCore.Components.Routing.NavLink>), which renders navigation links to other Razor components. The <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.
* `NavMenu.razor.css`: Stylesheet for the app's navigation menu.
* `SurveyPrompt` component (`SurveyPrompt.razor`): Blazor survey component.
* `wwwroot` folder: The [Web Root](xref:fundamentals/index#web-root) folder for the app containing the app's public static assets.
2022-03-15 17:53:00 +08:00
* `_Imports.razor`: Includes common Razor directives to include in the app's components (`.razor`), such as [`@using`](xref:mvc/views/razor#using) directives for namespaces.
2022-03-15 17:53:00 +08:00
* `App.razor`: The root component of the app that sets up client-side routing using the <xref:Microsoft.AspNetCore.Components.Routing.Router> component. The <xref:Microsoft.AspNetCore.Components.Routing.Router> component intercepts browser navigation and renders the page that matches the requested address.
2022-03-15 17:53:00 +08:00
* `appsettings.json` and environmental app settings files: Provide [configuration settings](xref:blazor/fundamentals/configuration) for the app.
2022-03-15 17:53:00 +08:00
* `Program.cs`: The app's entry point that sets up the ASP.NET Core [host](xref:fundamentals/host/generic-host).
* `Startup.cs`: Contains the app's startup logic. The `Startup` class defines two methods:
* `ConfigureServices`: Configures the app's [dependency injection (DI)](xref:fundamentals/dependency-injection) services. Services are added by calling <xref:Microsoft.Extensions.DependencyInjection.ComponentServiceCollectionExtensions.AddServerSideBlazor%2A>, and the `WeatherForecastService` is added to the service container for use by the example `FetchData` component.
* `Configure`: Configures the app's request handling pipeline:
* <xref:Microsoft.AspNetCore.Builder.ComponentEndpointRouteBuilderExtensions.MapBlazorHub%2A> is called to set up an endpoint for the real-time connection with the browser. The connection is created with [SignalR](xref:signalr/introduction), which is a framework for adding real-time web functionality to apps.
* [`MapFallbackToPage("/_Host")`](xref:Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.MapFallbackToPage%2A) is called to set up the root page of the app (`Pages/_Host.cshtml`) and enable navigation.
Additional files and folders may appear in an app produced from a Blazor Server project template when additional options are configured. For example, generating an app with ASP.NET Core Identity includes additional assets for authentication and authorization features.
:::moniker-end
:::moniker range="< aspnetcore-5.0"
2021-08-09 03:43:46 +08:00
Blazor Server project template: `blazorserver`
The Blazor Server template creates the initial files and directory structure for a Blazor Server app. The app is populated with demonstration code for a `FetchData` component that loads data from a registered service, `WeatherForecastService`, and user interaction with a `Counter` component.
* `Data` folder: Contains the `WeatherForecast` class and implementation of the `WeatherForecastService` that provides example weather data to the app's `FetchData` component.
* `Pages` folder: Contains the Blazor app's routable Razor components (`.razor`) and the root Razor page of a Blazor Server app. The route for each page is specified using the [`@page`](xref:mvc/views/razor#page) directive. The template includes the following:
2021-08-09 03:43:46 +08:00
* `_Host.cshtml`: The root page of the app implemented as a Razor Page:
* When any page of the app is initially requested, this page is rendered and returned in the response.
* The Host page specifies where the root `App` component (`App.razor`) is rendered.
* `Counter` component (`Counter.razor`): Implements the Counter page.
* `Error` component (`Error.razor`): Rendered when an unhandled exception occurs in the app.
* `FetchData` component (`FetchData.razor`): Implements the Fetch data page.
* `Index` component (`Index.razor`): Implements the Home page.
* `Properties` folder: Holds [development environment configuration](xref:fundamentals/environments#development-and-launchsettingsjson) in the `launchSettings.json` file.
2021-08-09 03:43:46 +08:00
* `Shared` folder: Contains the following shared components:
2021-08-09 03:43:46 +08:00
* `MainLayout` component (`MainLayout.razor`): The app's [layout component](xref:blazor/components/layouts).
* `NavMenu` component (`NavMenu.razor`): Implements sidebar navigation. Includes the [`NavLink` component](xref:blazor/fundamentals/routing#navlink-and-navmenu-components) (<xref:Microsoft.AspNetCore.Components.Routing.NavLink>), which renders navigation links to other Razor components. The <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.
* `SurveyPrompt` component (`SurveyPrompt.razor`): Blazor survey component.
* `wwwroot` folder: The [Web Root](xref:fundamentals/index#web-root) folder for the app containing the app's public static assets.
2021-08-09 03:43:46 +08:00
* `_Imports.razor`: Includes common Razor directives to include in the app's components (`.razor`), such as [`@using`](xref:mvc/views/razor#using) directives for namespaces.
* `App.razor`: The root component of the app that sets up client-side routing using the <xref:Microsoft.AspNetCore.Components.Routing.Router> component. The <xref:Microsoft.AspNetCore.Components.Routing.Router> component intercepts browser navigation and renders the page that matches the requested address.
* `appsettings.json` and environmental app settings files: Provide [configuration settings](xref:blazor/fundamentals/configuration) for the app.
* `Program.cs`: The app's entry point that sets up the ASP.NET Core [host](xref:fundamentals/host/generic-host).
2021-08-09 03:43:46 +08:00
* `Startup.cs`: Contains the app's startup logic. The `Startup` class defines two methods:
* `ConfigureServices`: Configures the app's [dependency injection (DI)](xref:fundamentals/dependency-injection) services. Services are added by calling <xref:Microsoft.Extensions.DependencyInjection.ComponentServiceCollectionExtensions.AddServerSideBlazor%2A>, and the `WeatherForecastService` is added to the service container for use by the example `FetchData` component.
* `Configure`: Configures the app's request handling pipeline:
2021-08-09 03:43:46 +08:00
* <xref:Microsoft.AspNetCore.Builder.ComponentEndpointRouteBuilderExtensions.MapBlazorHub%2A> is called to set up an endpoint for the real-time connection with the browser. The connection is created with [SignalR](xref:signalr/introduction), which is a framework for adding real-time web functionality to apps.
* [`MapFallbackToPage("/_Host")`](xref:Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.MapFallbackToPage%2A) is called to set up the root page of the app (`Pages/_Host.cshtml`) and enable navigation.
Additional files and folders may appear in an app produced from a Blazor Server project template when additional options are configured. For example, generating an app with ASP.NET Core Identity includes additional assets for authentication and authorization features.
:::moniker-end
2021-08-09 03:43:46 +08:00
## Blazor WebAssembly
:::moniker range=">= aspnetcore-7.0"
2021-08-09 03:43:46 +08:00
Blazor WebAssembly project templates: `blazorwasm`, `blazorwasm-empty`
The Blazor WebAssembly templates create the initial files and directory structure for a Blazor WebAssembly app:
* If the `blazorwasm` template is used, the app is populated with the following:
* Demonstration code for a `FetchData` component that loads data from a static asset (`weather.json`) and user interaction with a `Counter` component.
* [Bootstrap](https://getbootstrap.com/) frontend toolkit.
* If the `blazorwasm-empty` template is used, the app is created without demonstration code and Bootstrap.
Project structure:
2021-08-09 03:43:46 +08:00
* `Pages` folder: Contains the Blazor app's routable Razor components (`.razor`). The route for each page is specified using the [`@page`](xref:mvc/views/razor#page) directive. The template includes the following components:
2021-08-09 03:43:46 +08:00
* `Counter` component (`Counter.razor`): Implements the Counter page.
* `FetchData` component (`FetchData.razor`): Implements the Fetch data page.
* `Index` component (`Index.razor`): Implements the Home page.
* `Properties` folder: Holds [development environment configuration](xref:fundamentals/environments#development-and-launchsettingsjson) in the `launchSettings.json` file.
2021-08-09 03:43:46 +08:00
2022-03-15 17:53:00 +08:00
* `Shared` folder: Contains the following shared components and stylesheets:
2021-08-09 03:43:46 +08:00
* `MainLayout` component (`MainLayout.razor`): The app's [layout component](xref:blazor/components/layouts).
2022-03-15 17:53:00 +08:00
* `MainLayout.razor.css`: Stylesheet for the app's main layout.
2021-08-09 03:43:46 +08:00
* `NavMenu` component (`NavMenu.razor`): Implements sidebar navigation. Includes the [`NavLink` component](xref:blazor/fundamentals/routing#navlink-and-navmenu-components) (<xref:Microsoft.AspNetCore.Components.Routing.NavLink>), which renders navigation links to other Razor components. The <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.
2022-03-15 17:53:00 +08:00
* `NavMenu.razor.css`: Stylesheet for the app's navigation menu.
* `SurveyPrompt` component (`SurveyPrompt.razor`) (*ASP.NET Core 7.0 or earlier*): Blazor survey component.
2021-08-09 03:43:46 +08:00
* `wwwroot` folder: The [Web Root](xref:fundamentals/index#web-root) folder for the app containing the app's public static assets, including `appsettings.json` and environmental app settings files for [configuration settings](xref:blazor/fundamentals/configuration). The `index.html` webpage is the root page of the app implemented as an HTML page:
2021-08-09 03:43:46 +08:00
* When any page of the app is initially requested, this page is rendered and returned in the response.
2022-03-15 17:53:00 +08:00
* The page specifies where the root `App` component is rendered. The component is rendered at the location of the `div` DOM element with an `id` of `app` (`<div id="app">Loading...</div>`).
2021-08-09 03:43:46 +08:00
* `_Imports.razor`: Includes common Razor directives to include in the app's components (`.razor`), such as [`@using`](xref:mvc/views/razor#using) directives for namespaces.
* `App.razor`: The root component of the app that sets up client-side routing using the <xref:Microsoft.AspNetCore.Components.Routing.Router> component. The <xref:Microsoft.AspNetCore.Components.Routing.Router> component intercepts browser navigation and renders the page that matches the requested address.
* `Program.cs`: The app's entry point that sets up the WebAssembly host:
2022-03-15 17:53:00 +08:00
* The `App` component is the root component of the app. The `App` component is specified as the `div` DOM element with an `id` of `app` (`<div id="app">Loading...</div>` in `wwwroot/index.html`) to the root component collection (`builder.RootComponents.Add<App>("#app")`).
2021-08-09 03:43:46 +08:00
* [Services](xref:blazor/fundamentals/dependency-injection) are added and configured (for example, `builder.Services.AddSingleton<IMyDependency, MyDependency>()`).
Additional files and folders may appear in an app produced from a Blazor WebAssembly project template when additional options are configured. For example, generating an app with ASP.NET Core Identity includes additional assets for authentication and authorization features.
:::moniker-end
:::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0"
2023-01-11 23:30:53 +08:00
A *hosted Blazor WebAssembly solution* includes the following ASP.NET Core projects:
* ":::no-loc text="Client":::": The Blazor WebAssembly app.
* ":::no-loc text="Server":::": An app that serves the Blazor WebAssembly app and weather data to clients.
* ":::no-loc text="Shared":::": A project that maintains common classes, methods, and resources.
The solution is generated from the Blazor WebAssembly project template in Visual Studio with the **ASP.NET Core Hosted** checkbox selected or with the `-ho|--hosted` option using the .NET CLI's `dotnet new blazorwasm` command. For more information, see <xref:blazor/tooling>.
The project structure of the client-side app in a hosted Blazor Webassembly solution (":::no-loc text="Client":::" project) is the same as the project structure for a standalone Blazor WebAssembly app. Additional files in a hosted Blazor WebAssembly solution:
* The ":::no-loc text="Server":::" project includes a weather forecast controller at `Controllers/WeatherForecastController.cs` that returns weather data to the ":::no-loc text="Client":::" project's `FetchData` component.
* The ":::no-loc text="Shared":::" project includes a weather forecast class at `WeatherForecast.cs` that represents weather data for the ":::no-loc text="Client":::" and ":::no-loc text="Server":::" projects.
2022-03-15 17:53:00 +08:00
:::moniker-end
:::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0"
2021-08-09 03:43:46 +08:00
Blazor WebAssembly project template: `blazorwasm`
2021-08-09 03:43:46 +08:00
The Blazor WebAssembly template creates the initial files and directory structure for a Blazor WebAssembly app. The app is populated with demonstration code for a `FetchData` component that loads data from a static asset, `weather.json`, and user interaction with a `Counter` component.
2021-08-09 03:43:46 +08:00
* `Pages` folder: Contains the Blazor app's routable Razor components (`.razor`). The route for each page is specified using the [`@page`](xref:mvc/views/razor#page) directive. The template includes the following components:
2021-08-09 03:43:46 +08:00
* `Counter` component (`Counter.razor`): Implements the Counter page.
* `FetchData` component (`FetchData.razor`): Implements the Fetch data page.
* `Index` component (`Index.razor`): Implements the Home page.
* `Properties` folder: Holds [development environment configuration](xref:fundamentals/environments#development-and-launchsettingsjson) in the `launchSettings.json` file.
2021-08-09 03:43:46 +08:00
2022-11-09 00:43:50 +08:00
* `Shared` folder: Contains the following shared components and stylesheets:
2021-08-09 03:43:46 +08:00
* `MainLayout` component (`MainLayout.razor`): The app's [layout component](xref:blazor/components/layouts).
2022-11-09 00:43:50 +08:00
* `MainLayout.razor.css`: Stylesheet for the app's main layout.
2021-08-09 03:43:46 +08:00
* `NavMenu` component (`NavMenu.razor`): Implements sidebar navigation. Includes the [`NavLink` component](xref:blazor/fundamentals/routing#navlink-and-navmenu-components) (<xref:Microsoft.AspNetCore.Components.Routing.NavLink>), which renders navigation links to other Razor components. The <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.
2022-11-09 00:43:50 +08:00
* `NavMenu.razor.css`: Stylesheet for the app's navigation menu.
2021-08-09 03:43:46 +08:00
* `SurveyPrompt` component (`SurveyPrompt.razor`): Blazor survey component.
* `wwwroot` folder: The [Web Root](xref:fundamentals/index#web-root) folder for the app containing the app's public static assets, including `appsettings.json` and environmental app settings files for [configuration settings](xref:blazor/fundamentals/configuration). The `index.html` webpage is the root page of the app implemented as an HTML page:
* When any page of the app is initially requested, this page is rendered and returned in the response.
* The page specifies where the root `App` component is rendered. The component is rendered at the location of the `div` DOM element with an `id` of `app` (`<div id="app">Loading...</div>`).
2021-08-09 03:43:46 +08:00
* `_Imports.razor`: Includes common Razor directives to include in the app's components (`.razor`), such as [`@using`](xref:mvc/views/razor#using) directives for namespaces.
* `App.razor`: The root component of the app that sets up client-side routing using the <xref:Microsoft.AspNetCore.Components.Routing.Router> component. The <xref:Microsoft.AspNetCore.Components.Routing.Router> component intercepts browser navigation and renders the page that matches the requested address.
* `Program.cs`: The app's entry point that sets up the WebAssembly host:
* The `App` component is the root component of the app. The `App` component is specified as the `div` DOM element with an `id` of `app` (`<div id="app">Loading...</div>` in `wwwroot/index.html`) to the root component collection (`builder.RootComponents.Add<App>("#app")`).
* [Services](xref:blazor/fundamentals/dependency-injection) are added and configured (for example, `builder.Services.AddSingleton<IMyDependency, MyDependency>()`).
2021-08-09 03:43:46 +08:00
Additional files and folders may appear in an app produced from a Blazor WebAssembly project template when additional options are configured. For example, generating an app with ASP.NET Core Identity includes additional assets for authentication and authorization features.
2021-08-09 03:43:46 +08:00
A *hosted Blazor WebAssembly solution* includes the following ASP.NET Core projects:
2021-08-09 03:43:46 +08:00
* ":::no-loc text="Client":::": The Blazor WebAssembly app.
* ":::no-loc text="Server":::": An app that serves the Blazor WebAssembly app and weather data to clients.
* ":::no-loc text="Shared":::": A project that maintains common classes, methods, and resources.
2021-08-09 03:43:46 +08:00
The solution is generated from the Blazor WebAssembly project template in Visual Studio with the **ASP.NET Core Hosted** checkbox selected or with the `-ho|--hosted` option using the .NET CLI's `dotnet new blazorwasm` command. For more information, see <xref:blazor/tooling>.
The project structure of the client-side app in a hosted Blazor Webassembly solution (":::no-loc text="Client":::" project) is the same as the project structure for a standalone Blazor WebAssembly app. Additional files in a hosted Blazor WebAssembly solution:
* The ":::no-loc text="Server":::" project includes a weather forecast controller at `Controllers/WeatherForecastController.cs` that returns weather data to the ":::no-loc text="Client":::" project's `FetchData` component.
* The ":::no-loc text="Shared":::" project includes a weather forecast class at `WeatherForecast.cs` that represents weather data for the ":::no-loc text="Client":::" and ":::no-loc text="Server":::" projects.
:::moniker-end
:::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0"
2022-03-15 17:53:00 +08:00
Blazor WebAssembly project template: `blazorwasm`
The Blazor WebAssembly template creates the initial files and directory structure for a Blazor WebAssembly app. The app is populated with demonstration code for a `FetchData` component that loads data from a static asset, `weather.json`, and user interaction with a `Counter` component.
* `Pages` folder: Contains the Blazor app's routable Razor components (`.razor`). The route for each page is specified using the [`@page`](xref:mvc/views/razor#page) directive. The template includes the following components:
2022-03-15 17:53:00 +08:00
* `Counter` component (`Counter.razor`): Implements the Counter page.
* `FetchData` component (`FetchData.razor`): Implements the Fetch data page.
* `Index` component (`Index.razor`): Implements the Home page.
* `Properties` folder: Holds [development environment configuration](xref:fundamentals/environments#development-and-launchsettingsjson) in the `launchSettings.json` file.
2022-03-15 17:53:00 +08:00
2022-11-09 00:43:50 +08:00
* `Shared` folder: Contains the following shared components and stylesheets:
2022-03-15 17:53:00 +08:00
* `MainLayout` component (`MainLayout.razor`): The app's [layout component](xref:blazor/components/layouts).
2022-11-09 00:43:50 +08:00
* `MainLayout.razor.css`: Stylesheet for the app's main layout.
2022-03-15 17:53:00 +08:00
* `NavMenu` component (`NavMenu.razor`): Implements sidebar navigation. Includes the [`NavLink` component](xref:blazor/fundamentals/routing#navlink-and-navmenu-components) (<xref:Microsoft.AspNetCore.Components.Routing.NavLink>), which renders navigation links to other Razor components. The <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.
2022-11-09 00:43:50 +08:00
* `NavMenu.razor.css`: Stylesheet for the app's navigation menu.
2022-03-15 17:53:00 +08:00
* `SurveyPrompt` component (`SurveyPrompt.razor`): Blazor survey component.
* `wwwroot` folder: The [Web Root](xref:fundamentals/index#web-root) folder for the app containing the app's public static assets, including `appsettings.json` and environmental app settings files for [configuration settings](xref:blazor/fundamentals/configuration). The `index.html` webpage is the root page of the app implemented as an HTML page:
2022-03-15 17:53:00 +08:00
* When any page of the app is initially requested, this page is rendered and returned in the response.
2022-11-09 00:43:50 +08:00
* The page specifies where the root `App` component is rendered. The component is rendered at the location of the `div` DOM element with an `id` of `app` (`<div id="app">Loading...</div>`).
2022-03-15 17:53:00 +08:00
* `_Imports.razor`: Includes common Razor directives to include in the app's components (`.razor`), such as [`@using`](xref:mvc/views/razor#using) directives for namespaces.
* `App.razor`: The root component of the app that sets up client-side routing using the <xref:Microsoft.AspNetCore.Components.Routing.Router> component. The <xref:Microsoft.AspNetCore.Components.Routing.Router> component intercepts browser navigation and renders the page that matches the requested address.
* `Program.cs`: The app's entry point that sets up the WebAssembly host:
2022-11-09 00:43:50 +08:00
* The `App` component is the root component of the app. The `App` component is specified as the `div` DOM element with an `id` of `app` (`<div id="app">Loading...</div>` in `wwwroot/index.html`) to the root component collection (`builder.RootComponents.Add<App>("#app")`).
2022-03-15 17:53:00 +08:00
* [Services](xref:blazor/fundamentals/dependency-injection) are added and configured (for example, `builder.Services.AddSingleton<IMyDependency, MyDependency>()`).
Additional files and folders may appear in an app produced from a Blazor WebAssembly project template when additional options are configured. For example, generating an app with ASP.NET Core Identity includes additional assets for authentication and authorization features.
2023-01-11 23:30:53 +08:00
A *hosted Blazor WebAssembly solution* includes the following ASP.NET Core projects:
* ":::no-loc text="Client":::": The Blazor WebAssembly app.
* ":::no-loc text="Server":::": An app that serves the Blazor WebAssembly app and weather data to clients.
* ":::no-loc text="Shared":::": A project that maintains common classes, methods, and resources.
The solution is generated from the Blazor WebAssembly project template in Visual Studio with the **ASP.NET Core Hosted** checkbox selected or with the `-ho|--hosted` option using the .NET CLI's `dotnet new blazorwasm` command. For more information, see <xref:blazor/tooling>.
The project structure of the client-side app in a hosted Blazor Webassembly solution (":::no-loc text="Client":::" project) is the same as the project structure for a standalone Blazor WebAssembly app. Additional files in a hosted Blazor WebAssembly solution:
* The ":::no-loc text="Server":::" project includes a weather forecast controller at `Controllers/WeatherForecastController.cs` that returns weather data to the ":::no-loc text="Client":::" project's `FetchData` component.
* The ":::no-loc text="Shared":::" project includes a weather forecast class at `WeatherForecast.cs` that represents weather data for the ":::no-loc text="Client":::" and ":::no-loc text="Server":::" projects.
:::moniker-end
2022-11-09 00:43:50 +08:00
:::moniker range="< aspnetcore-5.0"
2022-11-09 00:43:50 +08:00
Blazor WebAssembly project template: `blazorwasm`
2022-07-28 20:53:28 +08:00
2022-11-09 00:43:50 +08:00
The Blazor WebAssembly template creates the initial files and directory structure for a Blazor WebAssembly app. The app is populated with demonstration code for a `FetchData` component that loads data from a static asset, `weather.json`, and user interaction with a `Counter` component.
* `Pages` folder: Contains the Blazor app's routable Razor components (`.razor`). The route for each page is specified using the [`@page`](xref:mvc/views/razor#page) directive. The template includes the following components:
* `Counter` component (`Counter.razor`): Implements the Counter page.
* `FetchData` component (`FetchData.razor`): Implements the Fetch data page.
* `Index` component (`Index.razor`): Implements the Home page.
* `Properties` folder: Holds [development environment configuration](xref:fundamentals/environments#development-and-launchsettingsjson) in the `launchSettings.json` file.
2022-11-09 00:43:50 +08:00
* `Shared` folder: Contains the following shared components:
* `MainLayout` component (`MainLayout.razor`): The app's [layout component](xref:blazor/components/layouts).
* `NavMenu` component (`NavMenu.razor`): Implements sidebar navigation. Includes the [`NavLink` component](xref:blazor/fundamentals/routing#navlink-and-navmenu-components) (<xref:Microsoft.AspNetCore.Components.Routing.NavLink>), which renders navigation links to other Razor components. The <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.
* `SurveyPrompt` component (`SurveyPrompt.razor`): Blazor survey component.
* `wwwroot` folder: The [Web Root](xref:fundamentals/index#web-root) folder for the app containing the app's public static assets, including `appsettings.json` and environmental app settings files for [configuration settings](xref:blazor/fundamentals/configuration). The `index.html` webpage is the root page of the app implemented as an HTML page:
* When any page of the app is initially requested, this page is rendered and returned in the response.
2022-11-09 00:43:50 +08:00
* The page specifies where the root `App` component is rendered. The component is rendered at the location of the `app` DOM element (`<app>Loading...</app>`).
* `_Imports.razor`: Includes common Razor directives to include in the app's components (`.razor`), such as [`@using`](xref:mvc/views/razor#using) directives for namespaces.
* `App.razor`: The root component of the app that sets up client-side routing using the <xref:Microsoft.AspNetCore.Components.Routing.Router> component. The <xref:Microsoft.AspNetCore.Components.Routing.Router> component intercepts browser navigation and renders the page that matches the requested address.
* `Program.cs`: The app's entry point that sets up the WebAssembly host:
2022-11-09 00:43:50 +08:00
* The `App` component is the root component of the app. The `App` component is specified as the `app` DOM element (`<app>Loading...</app>` in `wwwroot/index.html`) to the root component collection (`builder.RootComponents.Add<App>("app")`).
* [Services](xref:blazor/fundamentals/dependency-injection) are added and configured (for example, `builder.Services.AddSingleton<IMyDependency, MyDependency>()`).
Additional files and folders may appear in an app produced from a Blazor WebAssembly project template when additional options are configured. For example, generating an app with ASP.NET Core Identity includes additional assets for authentication and authorization features.
2023-01-11 23:30:53 +08:00
A *hosted Blazor WebAssembly solution* includes the following ASP.NET Core projects:
* ":::no-loc text="Client":::": The Blazor WebAssembly app.
* ":::no-loc text="Server":::": An app that serves the Blazor WebAssembly app and weather data to clients.
* ":::no-loc text="Shared":::": A project that maintains common classes, methods, and resources.
The solution is generated from the Blazor WebAssembly project template in Visual Studio with the **ASP.NET Core Hosted** checkbox selected or with the `-ho|--hosted` option using the .NET CLI's `dotnet new blazorwasm` command. For more information, see <xref:blazor/tooling>.
The project structure of the client-side app in a hosted Blazor Webassembly solution (":::no-loc text="Client":::" project) is the same as the project structure for a standalone Blazor WebAssembly app. Additional files in a hosted Blazor WebAssembly solution:
* The ":::no-loc text="Server":::" project includes a weather forecast controller at `Controllers/WeatherForecastController.cs` that returns weather data to the ":::no-loc text="Client":::" project's `FetchData` component.
* The ":::no-loc text="Shared":::" project includes a weather forecast class at `WeatherForecast.cs` that represents weather data for the ":::no-loc text="Client":::" and ":::no-loc text="Server":::" projects.
:::moniker-end
## Location of `<head>` content
:::moniker range=">= aspnetcore-8.0"
In a Blazor Web App, `<head>` content is located in the `App.razor` file.
:::moniker-end
:::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0"
In a Blazor Server app, `<head>` content is located in the `Pages/_Host.cshtml` file.
:::moniker-end
:::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0"
In a Blazor Server app, `<head>` content is located in the `Pages/_Layout.cshtml` file.
:::moniker-end
:::moniker range="< aspnetcore-6.0"
In a Blazor Server app, `<head>` content is located in the `Pages/_Host.cshtml` file.
:::moniker-end
In a Blazor WebAssembly app, `<head>` content is located in the `wwwroot/index.html` file.
:::moniker range="< aspnetcore-8.0"
## Dual Blazor Server/Blazor WebAssembly app
To create an app that can run as either a Blazor Server app or a Blazor WebAssembly app, one approach is to place all of the app logic and components into a [Razor class library (RCL)](xref:blazor/components/class-libraries) and reference the RCL from separate Blazor Server and Blazor WebAssembly projects. For common services whose implementations differ based on the hosting model, define the service interfaces in the RCL and implement the services in the Blazor Server and Blazor WebAssembly projects.
:::moniker-end
2021-08-09 03:43:46 +08:00
## Additional resources
:::moniker range=">= aspnetcore-7.0"
* <xref:blazor/tooling>
* <xref:blazor/hosting-models>
* <xref:fundamentals/minimal-apis>
* [Blazor samples GitHub repository (`dotnet/blazor-samples`)](https://github.com/dotnet/blazor-samples)
:::moniker-end
:::moniker range="< aspnetcore-7.0"
2021-08-09 03:43:46 +08:00
* <xref:blazor/tooling>
* <xref:blazor/hosting-models>
* [Blazor samples GitHub repository (`dotnet/blazor-samples`)](https://github.com/dotnet/blazor-samples)
2021-08-09 03:43:46 +08:00
:::moniker-end