ASP.NET Core Blazor project structure
This article describes the files and folders that make up a Blazor app generated from one of the Blazor framework's project templates.
Blazor WebAssembly
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.
Pagesfolder: Contains the routable components/pages (.razor) that make up the Blazor app. The route for each page is specified using the@pagedirective. The template includes the following components:Countercomponent (Counter.razor): Implements the Counter page.FetchDatacomponent (FetchData.razor): Implements the Fetch data page.Indexcomponent (Index.razor): Implements the Home page.
Properties/launchSettings.json: Holds development environment configuration.Sharedfolder: Contains the following shared components and stylesheets:MainLayoutcomponent (MainLayout.razor): The app's layout component.MainLayout.razor.css: Stylesheet for the app's main layout.NavMenucomponent (NavMenu.razor): Implements sidebar navigation. Includes theNavLinkcomponent (NavLink), which renders navigation links to other Razor components. The 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.SurveyPromptcomponent (SurveyPrompt.razor): Blazor survey component.
wwwroot: The Web Root folder for the app containing the app's public static assets, includingappsettings.jsonand environmental app settings files for configuration settings. Theindex.htmlwebpage 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
Appcomponent is rendered. The component is rendered at the location of thedivDOM element with anidofapp(<div id="app">Loading...</div>).
Note
JavaScript (JS) files added to the wwwroot/index.html file should appear before the closing </body> tag. The order that custom JS code is loaded from JS files is important in some scenarios. For example, ensure that JS files with interop methods are included before Blazor framework JS files.
_Imports.razor: Includes common Razor directives to include in the app's components (.razor), such as@usingdirectives for namespaces.App.razor: The root component of the app that sets up client-side routing using the Router component. The 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
Appcomponent is the root component of the app. TheAppcomponent is specified as thedivDOM element with anidofapp(<div id="app">Loading...</div>inwwwroot/index.html) to the root component collection (builder.RootComponents.Add<App>("#app")). - Services are added and configured (for example,
builder.Services.AddSingleton<IMyDependency, MyDependency>()).
- The
Blazor Server
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.
Datafolder: Contains theWeatherForecastclass and implementation of theWeatherForecastServicethat provides example weather data to the app'sFetchDatacomponent.Pagesfolder: Contains the routable components/pages (.razor) that make up the Blazor app and the root Razor page of a Blazor Server app. The route for each page is specified using the@pagedirective. 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
Appcomponent (App.razor) is rendered.
_Layout.cshtml: The layout page for the_Host.cshtmlroot page of the app.Countercomponent (Counter.razor): Implements the Counter page.Errorcomponent (Error.razor): Rendered when an unhandled exception occurs in the app.FetchDatacomponent (FetchData.razor): Implements the Fetch data page.Indexcomponent (Index.razor): Implements the Home page.
Note
JavaScript (JS) files added to the Pages/_Layout.cshtml file should appear before the closing </body> tag. The order that custom JS code is loaded from JS files is important in some scenarios. For example, ensure that JS files with interop methods are included before Blazor framework JS files.
Properties/launchSettings.json: Holds development environment configuration.Sharedfolder: Contains the following shared components and stylesheets:MainLayoutcomponent (MainLayout.razor): The app's layout component.MainLayout.razor.css: Stylesheet for the app's main layout.NavMenucomponent (NavMenu.razor): Implements sidebar navigation. Includes theNavLinkcomponent (NavLink), which renders navigation links to other Razor components. The 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.SurveyPromptcomponent (SurveyPrompt.razor): Blazor survey component.
wwwroot: The 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@usingdirectives for namespaces.App.razor: The root component of the app that sets up client-side routing using the Router component. The Router component intercepts browser navigation and renders the page that matches the requested address.appsettings.jsonand environmental app settings files: Provide configuration settings for the app.Program.cs: The app's entry point that sets up the ASP.NET Core host and contains the app's startup logic, including service registrations and request processing pipeline configuration:- Specifies the app's dependency injection (DI) services. Services are added by calling AddServerSideBlazor, and the
WeatherForecastServiceis added to the service container for use by the exampleFetchDatacomponent. - Configures the app's request handling pipeline:
- MapBlazorHub is called to set up an endpoint for the real-time connection with the browser. The connection is created with SignalR, which is a framework for adding real-time web functionality to apps.
MapFallbackToPage("/_Host")is called to set up the root page of the app (Pages/_Host.cshtml) and enable navigation.
- Specifies the app's dependency injection (DI) services. Services are added by calling AddServerSideBlazor, and the
Additional resources
This article describes the files and folders that make up a Blazor app generated from one of the Blazor framework's project templates. For information on how to use tooling to create a Blazor app from a Blazor project template, see Tooling for ASP.NET Core Blazor. For information on Blazor's hosting models, Blazor WebAssembly and Blazor Server, see ASP.NET Core Blazor hosting models.
Blazor WebAssembly
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.
Pagesfolder: Contains the routable components/pages (.razor) that make up the Blazor app. The route for each page is specified using the@pagedirective. The template includes the following components:Countercomponent (Counter.razor): Implements the Counter page.FetchDatacomponent (FetchData.razor): Implements the Fetch data page.Indexcomponent (Index.razor): Implements the Home page.
Properties/launchSettings.json: Holds development environment configuration.Sharedfolder: Contains the following shared components and stylesheets:MainLayoutcomponent (MainLayout.razor): The app's layout component.MainLayout.razor.css: Stylesheet for the app's main layout.NavMenucomponent (NavMenu.razor): Implements sidebar navigation. Includes theNavLinkcomponent (NavLink), which renders navigation links to other Razor components. The 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.SurveyPromptcomponent (SurveyPrompt.razor): Blazor survey component.
wwwroot: The Web Root folder for the app containing the app's public static assets, includingappsettings.jsonand environmental app settings files for configuration settings. Theindex.htmlwebpage 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
Appcomponent is rendered. The component is rendered at the location of thedivDOM element with anidofapp(<div id="app">Loading...</div>).
Note
JavaScript (JS) files added to the wwwroot/index.html file should appear before the closing </body> tag. The order that custom JS code is loaded from JS files is important in some scenarios. For example, ensure that JS files with interop methods are included before Blazor framework JS files.
_Imports.razor: Includes common Razor directives to include in the app's components (.razor), such as@usingdirectives for namespaces.App.razor: The root component of the app that sets up client-side routing using the Router component. The 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
Appcomponent is the root component of the app. TheAppcomponent is specified as thedivDOM element with anidofapp(<div id="app">Loading...</div>inwwwroot/index.html) to the root component collection (builder.RootComponents.Add<App>("#app")). - Services are added and configured (for example,
builder.Services.AddSingleton<IMyDependency, MyDependency>()).
- The
Blazor Server
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.
Datafolder: Contains theWeatherForecastclass and implementation of theWeatherForecastServicethat provides example weather data to the app'sFetchDatacomponent.Pagesfolder: Contains the routable components/pages (.razor) that make up the Blazor app and the root Razor page of a Blazor Server app. The route for each page is specified using the@pagedirective. 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
Appcomponent (App.razor) is rendered.
Countercomponent (Counter.razor): Implements the Counter page.Errorcomponent (Error.razor): Rendered when an unhandled exception occurs in the app.FetchDatacomponent (FetchData.razor): Implements the Fetch data page.Indexcomponent (Index.razor): Implements the Home page.
Note
JavaScript (JS) files added to the Pages/_Host.cshtml file should appear before the closing </body> tag. The order that custom JS code is loaded from JS files is important in some scenarios. For example, ensure that JS files with interop methods are included before Blazor framework JS files.
Properties/launchSettings.json: Holds development environment configuration.Sharedfolder: Contains the following shared components and stylesheets:MainLayoutcomponent (MainLayout.razor): The app's layout component.MainLayout.razor.css: Stylesheet for the app's main layout.NavMenucomponent (NavMenu.razor): Implements sidebar navigation. Includes theNavLinkcomponent (NavLink), which renders navigation links to other Razor components. The 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.SurveyPromptcomponent (SurveyPrompt.razor): Blazor survey component.
wwwroot: The 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@usingdirectives for namespaces.App.razor: The root component of the app that sets up client-side routing using the Router component. The Router component intercepts browser navigation and renders the page that matches the requested address.appsettings.jsonand environmental app settings files: Provide configuration settings for the app.Program.cs: The app's entry point that sets up the ASP.NET Core host.Startup.cs: Contains the app's startup logic. TheStartupclass defines two methods:ConfigureServices: Configures the app's dependency injection (DI) services. Services are added by calling AddServerSideBlazor, and theWeatherForecastServiceis added to the service container for use by the exampleFetchDatacomponent.Configure: Configures the app's request handling pipeline:- MapBlazorHub is called to set up an endpoint for the real-time connection with the browser. The connection is created with SignalR, which is a framework for adding real-time web functionality to apps.
MapFallbackToPage("/_Host")is called to set up the root page of the app (Pages/_Host.cshtml) and enable navigation.
Additional resources
This article describes the files and folders that make up a Blazor app generated from one of the Blazor framework's project templates. For information on how to use tooling to create a Blazor app from a Blazor project template, see Tooling for ASP.NET Core Blazor. For information on Blazor's hosting models, Blazor WebAssembly and Blazor Server, see ASP.NET Core Blazor hosting models.
Blazor WebAssembly
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.
Pagesfolder: Contains the routable components/pages (.razor) that make up the Blazor app. The route for each page is specified using the@pagedirective. The template includes the following components:Countercomponent (Counter.razor): Implements the Counter page.FetchDatacomponent (FetchData.razor): Implements the Fetch data page.Indexcomponent (Index.razor): Implements the Home page.
Properties/launchSettings.json: Holds development environment configuration.Sharedfolder: Contains the following shared components:MainLayoutcomponent (MainLayout.razor): The app's layout component.NavMenucomponent (NavMenu.razor): Implements sidebar navigation. Includes theNavLinkcomponent (NavLink), which renders navigation links to other Razor components. The NavLink component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.SurveyPromptcomponent (SurveyPrompt.razor): Blazor survey component.
wwwroot: The Web Root folder for the app containing the app's public static assets, includingappsettings.jsonand environmental app settings files for configuration settings. Theindex.htmlwebpage 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
Appcomponent is rendered. The component is rendered at the location of theappDOM element (<app>Loading...</app>).
Note
JavaScript (JS) files added to the wwwroot/index.html file should appear before the closing </body> tag. The order that custom JS code is loaded from JS files is important in some scenarios. For example, ensure that JS files with interop methods are included before Blazor framework JS files.
_Imports.razor: Includes common Razor directives to include in the app's components (.razor), such as@usingdirectives for namespaces.App.razor: The root component of the app that sets up client-side routing using the Router component. The 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
Appcomponent is the root component of the app. TheAppcomponent is specified as theappDOM element (<app>Loading...</app>inwwwroot/index.html) to the root component collection (builder.RootComponents.Add<App>("app")). - Services are added and configured (for example,
builder.Services.AddSingleton<IMyDependency, MyDependency>()).
- The
Blazor Server
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.
Datafolder: Contains theWeatherForecastclass and implementation of theWeatherForecastServicethat provides example weather data to the app'sFetchDatacomponent.Pagesfolder: Contains the routable components/pages (.razor) that make up the Blazor app and the root Razor page of a Blazor Server app. The route for each page is specified using the@pagedirective. 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
Appcomponent (App.razor) is rendered.
Countercomponent (Counter.razor): Implements the Counter page.Errorcomponent (Error.razor): Rendered when an unhandled exception occurs in the app.FetchDatacomponent (FetchData.razor): Implements the Fetch data page.Indexcomponent (Index.razor): Implements the Home page.
Note
JavaScript (JS) files added to the Pages/_Host.cshtml file should appear before the closing </body> tag. The order that custom JS code is loaded from JS files is important in some scenarios. For example, ensure that JS files with interop methods are included before Blazor framework JS files.
Properties/launchSettings.json: Holds development environment configuration.Sharedfolder: Contains the following shared components:MainLayoutcomponent (MainLayout.razor): The app's layout component.NavMenucomponent (NavMenu.razor): Implements sidebar navigation. Includes theNavLinkcomponent (NavLink), which renders navigation links to other Razor components. The NavLink component automatically indicates a selected state when its component is loaded, which helps the user understand which component is currently displayed.SurveyPromptcomponent (SurveyPrompt.razor): Blazor survey component.
wwwroot: The 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@usingdirectives for namespaces.App.razor: The root component of the app that sets up client-side routing using the Router component. The Router component intercepts browser navigation and renders the page that matches the requested address.appsettings.jsonand environmental app settings files: Provide configuration settings for the app.Program.cs: The app's entry point that sets up the ASP.NET Core host.Startup.cs: Contains the app's startup logic. TheStartupclass defines two methods:ConfigureServices: Configures the app's dependency injection (DI) services. Services are added by calling AddServerSideBlazor, and theWeatherForecastServiceis added to the service container for use by the exampleFetchDatacomponent.Configure: Configures the app's request handling pipeline:- MapBlazorHub is called to set up an endpoint for the real-time connection with the browser. The connection is created with SignalR, which is a framework for adding real-time web functionality to apps.
MapFallbackToPage("/_Host")is called to set up the root page of the app (Pages/_Host.cshtml) and enable navigation.