Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article explains how to host and deploy Blazor apps.
Publish the app
Apps are published for deployment in Release configuration.
Note
Publish a hosted Blazor WebAssembly solution from the Server project.
- Select the Publish {APPLICATION} command from the Build menu, where the
{APPLICATION}placeholder the app's name. - Select the publish target. To publish locally, select Folder. Select Next.
- When publishing locally, accept the default folder location or specify a different location. Select Finish to save the profile. Select Close.
- To clean the target's publish folder prior to publishing the app, select Show all settings. Select Settings > File Publish Options > Delete all existing files prior to publish. Select Save.
- Select the Publish button.
Publishing the app triggers a restore of the project's dependencies and builds the project before creating the assets for deployment. As part of the build process, unused methods and assemblies are removed to reduce app download size and load times.
Empty the target publish folder
When using the dotnet publish command in a command shell to publish an app, the command generates the necessary files for deployment based on the current state of the project and places the files into the specified output folder. The command doesn't automatically clean the target folder before publishing the app.
To empty the target folder automatically before the app is published, add the following MSBuild target to the app's project file (.csproj) under the root <Project> element:
<Target Name="_RemovePublishDirBeforePublishing" BeforeTargets="BeforePublish">
<RemoveDir Directories="$(PublishDir)" Condition="'$(PublishDir)' != ''" />
</Target>
Default publish locations
- Blazor Web App: The app is published into the
/bin/Release/{TARGET FRAMEWORK}/publishfolder, where the{TARGET FRAMEWORK}placeholder is the target framework. Deploy the contents of thepublishfolder to the host. - Standalone Blazor WebAssembly: The app is published into the
bin/Release/{TARGET FRAMEWORK}/publishorbin/Release/{TARGET FRAMEWORK}/browser-wasm/publishfolder. To deploy the app as a static site, copy the contents of thewwwrootfolder to the static site host.
- Blazor Server: The app is published into the
/bin/Release/{TARGET FRAMEWORK}/publishfolder, where the{TARGET FRAMEWORK}placeholder is the target framework.. Deploy the contents of thepublishfolder to the host. - Blazor WebAssembly
- Standalone: The app is published into the
/bin/Release/{TARGET FRAMEWORK}/publishorbin/Release/{TARGET FRAMEWORK}/browser-wasm/publishfolder. To deploy the app as a static site, copy the contents of thewwwrootfolder to the static site host. - Hosted: The server ASP.NET Core app and client Blazor WebAssembly app are published into the
/bin/Release/{TARGET FRAMEWORK}/publishfolder of the server app, along with any static web assets of the client app. Deploy the contents of thepublishfolder to the host.
- Standalone: The app is published into the
IIS
To host a Blazor app in IIS, see the following resources:
- IIS hosting
- Host and deploy ASP.NET Core server-side Blazor apps: Blazor Web Apps (.NET 8 or later) and Blazor Server apps (.NET 7 or earlier) running on IIS, including IIS with Azure Virtual Machines (VMs) running Windows OS and Azure App Service.
- Host and deploy ASP.NET Core Blazor WebAssembly with IIS: Standalone Blazor WebAssembly apps (all .NET releases) and hosted Blazor WebAssembly apps (.NET 7 or earlier).
- IIS sub-application hosting
- Follow the app base path guidance prior to publishing the app. The examples use an app base path of
/CoolAppand show how to obtain the base path from app settings or other configuration providers. - Follow the sub-application configuration guidance in Advanced configuration. The sub-app's folder path under the root site becomes the virtual path of the sub-app. For an app base path of
/CoolApp, the Blazor app is placed in a folder namedCoolAppunder the root site and the sub-app takes on a virtual path of/CoolApp.
- Follow the app base path guidance prior to publishing the app. The examples use an app base path of
Sharing an app pool among ASP.NET Core apps isn't supported, including for Blazor apps. Use one app pool per app when hosting with IIS, and avoid the use of IIS's virtual directories for hosting multiple apps.
One or more Blazor WebAssembly apps hosted by an ASP.NET Core app, known as a hosted Blazor WebAssembly solution, are supported for one app pool. However, we don't recommend or support assigning a single app pool to multiple hosted Blazor WebAssembly solutions or in sub-app hosting scenarios.
For more information on solutions, see Tooling for ASP.NET Core Blazor.
Aspects of Blazor WebAssembly caching apply to Blazor Web Apps
Blazor bundle caching and HTTP caching guidance in the Blazor WebAssembly node focus on standalone Blazor WebAssembly apps, but several aspects of client-side caching in these articles also apply to Blazor Web Apps that adopt Interactive WebAssembly or Interactive Auto render modes. If a Blazor Web App that renders content client-side encounters a static asset or bundle caching problem, see the guidance in these articles to troubleshoot the problem:
Blazor Server MapFallbackToPage configuration
This section only applies to Blazor Server apps. MapFallbackToPage isn't supported in Blazor Web Apps and Blazor WebAssembly apps.
In scenarios where an app requires a separate area with custom resources and Razor components:
Create a folder within the app's
Pagesfolder to hold the resources. For example, an administrator section of an app is created in a new folder namedAdmin(Pages/Admin).Create a root page (
_Host.cshtml) for the area. For example, create aPages/Admin/_Host.cshtmlfile from the app's main root page (Pages/_Host.cshtml). Don't provide an@pagedirective in the Admin_Hostpage.Add a layout to the area's folder (for example,
Pages/Admin/_Layout.razor). In the layout for the separate area, set the<base>taghrefto match the area's folder (for example,<base href="/Admin/" />). For demonstration purposes, add~/to the static resources in the page. For example:~/css/bootstrap/bootstrap.min.css~/css/site.css~/BlazorSample.styles.css(the example app's namespace isBlazorSample)~/_framework/blazor.server.js(Blazor script)
If the area should have its own static asset folder, add the folder and specify its location to static file middleware in
Program.cs(for example,app.UseStaticFiles("/Admin/wwwroot")).Razor components are added to the area's folder. At a minimum, add an
Indexcomponent to the area folder with the correct@pagedirective for the area. For example, add aPages/Admin/Index.razorfile based on the app's defaultPages/Index.razorfile. Indicate the Admin area as the route template at the top of the file (@page "/admin"). Add additional components as needed. For example,Pages/Admin/Component1.razorwith an@pagedirective and route template of@page "/admin/component1.In
Program.cs, call MapFallbackToPage for the area's request path immediately before the fallback root page path to the_Hostpage:... app.UseRouting(); app.MapBlazorHub(); app.MapFallbackToPage("~/Admin/{*clientroutes:nonfile}", "/Admin/_Host"); app.MapFallbackToPage("/_Host"); app.Run();