Build a Windows Forms Blazor app

This tutorial shows you how to build and run a Windows Forms Blazor app. You learn how to:

  • Create a Windows Forms Blazor app project
  • Run the app on Windows



Blazor Hybrid has reached General Availability (GA) and is fully supported for production workloads. Visual Studio and Visual Studio for Mac are in prerelease for working on Blazor Hybrid apps and may be modified before final release. We recommend keeping Visual Studio 2022 Preview updated for the best tooling experience.

Visual Studio workload

If the .NET desktop development workload isn't installed, use the Visual Studio installer to install the workload. For more information, see Modify Visual Studio workloads, components, and language packs.

Visual Studio installer .NET desktop development workload selection.

Create a Windows Forms Blazor project

Start Visual Studio 2022 Preview.

In the Start Window, select Create a new project:

Create a new solution in Visual Studio.

In the Create a new project dialog, filter the Project type drop-down to Desktop. Select the C# project template for Windows Forms App and select the Next button:

Create a new project in Visual Studio.

In the Configure your new project dialog, set the Project name to WinFormsBlazor, choose a suitable location for the project, and select the Next button.

Configure the project.

In the Additional information dialog, select the framework version, which must be .NET 6.0 or later. Select the Create button:

The Additional Information dialog.

Use NuGet Package Manager to install the Microsoft.AspNetCore.Components.WebView.WindowsForms NuGet package:

Use Nuget Package Manager in Visual Studio to install the Microsoft.AspNetCore.Components.WebView.WindowsForms NuGet package.

In Solution Explorer, right-click the project's name, WinFormsBlazor and select Edit Project File to open the project file (WinFormsBlazor.csproj).

At the top of the project file, change the SDK to Microsoft.NET.Sdk.Razor:

<Project Sdk="Microsoft.NET.Sdk.Razor">

Save the changes to the project file (WinFormsBlazor.csproj).

Add an _Imports.razor file to the root of the project with an @using directive for Microsoft.AspNetCore.Components.Web.


@using Microsoft.AspNetCore.Components.Web

Add a wwwroot folder to the project.

Add an index.html file to the wwwroot folder with the following markup.


<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="/" />
    <link href="css/app.css" rel="stylesheet" />
    <link href="WinFormsBlazor.styles.css" rel="stylesheet" />


    <div id="app">Loading...</div>

    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>

    <script src="_framework/blazor.webview.js"></script>



Inside the wwwroot folder, create a css folder to hold stylesheets.

Add an app.css stylesheet to the wwwroot/css folder with the following content.


html, body {
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;

.valid.modified:not([type=checkbox]) {
    outline: 1px solid #26b050;

.invalid {
    outline: 1px solid red;

.validation-message {
    color: red;

#blazor-error-ui {
    background: lightyellow;
    bottom: 0;
    box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
    display: none;
    left: 0;
    padding: 0.6rem 1.25rem 0.7rem 1.25rem;
    position: fixed;
    width: 100%;
    z-index: 1000;

    #blazor-error-ui .dismiss {
        cursor: pointer;
        position: absolute;
        right: 0.75rem;
        top: 0.5rem;

Add the following Counter component to the root of the project, which is the default Counter component found in Blazor project templates.



<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()

In Solution Explorer, double-click on the Form1.cs file to open the designer:

The Form1.cs file in Solution Explorer.

Open the Toolbox by either selecting the Toolbox button along the left edge of the Visual Studio window or selecting the View > Toolbox menu item.

Locate the BlazorWebView control under Microsoft.AspNetCore.Components.WebView.WindowsForms. Drag the BlazorWebView from the Toolbox into the Form1 designer. Be careful not to accidentally drag a WebView2 control into the form.

BlazorWebView in the Toolbox.

Visual Studio shows the BlazorWebView control in the form designer as WebView2 and automatically names the control blazorWebView1:

BlazorWebView in the Form1 designer.

In Form1, select the BlazorWebView (WebView2) with a single click.

In the BlazorWebView's Properties, confirm that the control is named blazorWebView1. If the name isn't blazorWebView1, the wrong control was dragged from the Toolbox. Delete the WebView2 control in Form1 and drag the BlazorWebView control into the form.

The BlazorWebView is automatically named 'blazorWebView1' by Visual Studio.

In the control's properties, change the BlazorWebView's Dock value to Fill:

BlazorWebView properties with Dock set to Fill.

In the Form1 designer, right-click Form1 and select View Code.

Add namespaces for Microsoft.AspNetCore.Components.WebView.WindowsForms and Microsoft.Extensions.DependencyInjection to the top of the Form1.cs file:

using Microsoft.AspNetCore.Components.WebView.WindowsForms;
using Microsoft.Extensions.DependencyInjection;

Inside the Form1 constructor, after the InitializeComponent() method call, add the following code:

var services = new ServiceCollection();
blazorWebView1.HostPage = "wwwroot\\index.html";
blazorWebView1.Services = services.BuildServiceProvider();

The final, complete C# code of Form1.cs:

using Microsoft.AspNetCore.Components.WebView.WindowsForms;
using Microsoft.Extensions.DependencyInjection;

namespace WinFormsBlazor
    public partial class Form1 : Form
        public Form1()

            var services = new ServiceCollection();
            blazorWebView1.HostPage = "wwwroot\\index.html";
            blazorWebView1.Services = services.BuildServiceProvider();

Run the app

Select the start button in the Visual Studio toolbar:

Start button of the Visual Studio toolbar.

The app running on Windows:

The app running on Windows.

Next steps

In this tutorial, you learned how to:

  • Create a Windows Forms Blazor app project
  • Run the app on Windows

Learn more about Blazor Hybrid apps: