Telerik blogs

See how to integrate a Blazor WebAssembly project into an existing ASP.NET Core web application, using familiar C# code files and Razor syntax to implement your web UI.

When you think of developing a user interface for the interactive web, your thoughts may immediately turn to JavaScript. Consequently, for many C# developers, this would mean learning the ins and outs of an entirely new programming language.

However, this doesn’t have to be the case! Blazor WASM (aka Blazor WebAssembly) allows developers to implement web user interfaces using familiar C# code files and Razor syntax. These source files are then compiled into byte code for download to run atop the WebAssembly runtime in the client browser, yielding improved runtime performance due to the compilation.

A block diagram displays indicating Razor components written in .NET are compiled down to byte code and run on the WebAssembly runtime which in turn interacts with the DOM of the web page.
Image source

Blazor WebAssembly is a prevalent solution for single-page web applications (or SPAs) that adheres to the typical n-layer pattern of web application development. In the n-layer design, the user interface is implemented separately from the business logic and data access, with the latter two typically deployed as an API service.

Getting started with Blazor WASM in new greenfield projects is simplified as Visual Studio includes templates for Blazor WebAssembly projects, but what happens when you want to integrate Blazor WASM with an existing ASP.NET Core web application? How do we slowly migrate and modernize existing web applications to harness the power of Blazor WASM?

In this article, you will learn how to integrate a Blazor WebAssembly project into an existing ASP.NET Core web application. Furthermore, you’ll learn how to incorporate Progress Telerik UI for Blazor, which provides ready-to-use components for building compelling web user interface experiences with Blazor WebAssembly.

Step 1. Set up the Web Application and Blazor WASM Projects

In this demonstration, we’ll create two projects. The first project is an ASP.NET Core Web Application representing our existing web application. The second project is a Blazor WebAssembly project where we’ll build new Blazor components to integrate into our existing web application.

  1. Open Visual Studio 2022 and choose to Create a new project.

The Visual Studio 2022 dialog displays with the option to Create a new project highlighted.

  1. On the Create a new project dialog, search for and select ASP.NET Core Web App. Press Next.

The Create a new project dialog displays with ASP.NET Core Web App entered in the search bar and selected from the list of results. The Next button is selected.

  1. On the Configure your new project dialog, name the project Existing.Web, optionally choose the file system location and press Next.

The Configure your new project dialog displays with the project name set to Existing.Web and the next button is selected.

  1. On the Additional information dialog, select .NET 7.0 (Standard Term Support) for the Framework then press Create.

The Additional information dialog displays with .NET 7.0 selected as the framework and the Create button is highlighted.

Note: The guidance in this article will also work with .NET 6.0 projects. .NET 5 is out of support, so it is recommended to upgrade existing applications to at least .NET 6.0 first.

  1. In the Solution Explorer panel, right-click the solution and expand the Add menu. Select New Project.

The context menu of the solution displays with the Add menu item expanded and the New Project item selected.

  1. In the Add new project dialog, search for Blazor WebAssembly and select Blazor WebAssembly App from the search results. Press Next.

The Add a new project dialog displays with Blazor WebAssembly entered in the search box. The Blazor WebAssembly App item is selected from the search results and the Next button is highlighted.

  1. In the Configure your new project dialog, name the project BlazorApp and press Next.

The Configure your new project dialog displays with BlazorApp entered for the project name. The Next button is highlighted.

  1. On the Additional information dialog, select .NET 7.0 (Standard Term Support) for the Framework then press Create.

The Additional information dialog displays with .NET 7.0 selected as the framework and the Create button is highlighted.

Note: The guidance in this article will also work with .NET 6.0 projects. .NET 5 is out of support, so it is recommended to upgrade existing applications to at least .NET 6.0 first.

Step 2. Prepare the Blazor WebAssembly Application

The Blazor WebAssembly App template generates a single-page application (SPA). Therefore, when the Blazor application runs, it looks for an HTML element with the ID of app to contain and display the running application. This functionality needs to be disabled as, in this case, we want the existing web application to provide the overall user experience while allowing us to incorporate Blazor components developed in the Blazor WASM project.

  1. In the Solution Explorer panel, locate the BlazorApp project and open the Program.cs file.

  2. In the Program.cs file, comment out the following line of code and save the file:

builder.RootComponents.Add<App>("#app");

Visual Studio 2022 displays with the BlazorApp project expanded and the Program.cs code file selected. In the code editor window the line of code mentioned above is commented out.

Step 3. Prepare the Existing Web Application Project

In order for the existing ASP.NET Core web application to make use of the Blazor Web Assembly components, we will need a project reference, NuGet package and some infrastructure code.

  1. In the Solution Explorer panel, locate the Existing.Web project then right-click and expand the Add item. Select Project Reference.

The context menu for the Existing.Web project displays with the Add item expanded and the Project reference item selected.

  1. In the Reference Manager dialog, check the box next to BlazorApp and press OK.

The Reference Manager dialog displays with a checked box next to BlazorApp and the OK button is highlighted.

  1. Right-click the Existing.Web project once more and select Manage NuGet Packages.

The Solution Explorer panel displays with the context menu expanded and the Manage NuGet Packages item highlighted.

  1. On the NuGet screen, select the Browse tab and enter Microsoft.AspNetCore.Components.WebAssembly.Server into the search box. Select Microsoft.AspNetCore.Components.WebAssembly.Server from the search results and press Install. Accept any Licenses.

The NuGet screen displays with the Browse tab selected and Microsoft.AspNetCore.Components.WebAssembly.Server entered in the search box. The Microsoft.AspNetCore.Components.WebAssembly.Server item is selected from the search results and the Install button is highlighted.

  1. In the Solution Explorer panel, locate the Existing.Web project and open the Program.cs file.

The Solution Explorer panel displays with the Existing.Web project expanded and the Program.cs file selected.

  1. Add an else condition to the if (!app.Environment.IsDevelopment()) code block with the following code:
else
{
    app.UseWebAssemblyDebugging();
}

A portion of the Program.cs window displays with the code mentioned above highlighted.

  1. Directly below the if (!app.Environment.IsDevelopment()) code block, add the following code and save the file:
app.UseBlazorFrameworkFiles();
app.MapFallbackToFile("index.html");

A portion of the Program.cs window displays with the code mentioned above highlighted.

  1. In the Existing.Web project, expand the Pages folder. Expand the Shared folder, locate and open the _Layout.cshtml file.

The Solution Explorer panel displays with the Existing.Web project expanded along with the Pages and Shared folders. The _Layout.cshtml file is highlighted.

  1. Locate the script references close to the bottom of the page. Add the following script reference, and save the file:
<script src="_framework/blazor.webassembly.js"></script>

A portion of the code window for _Layout.cshtml displays with the aforementioned code highlighted.

Note: Adding the script to _Layout.cs will allow you to add Blazor components to any view. Alternatively, you can choose to only include the script on each specific view that Blazor components are needed.

Step 4. Add a Blazor Component to a View in the Existing Web Application

The Blazor WebAssembly App project comes with a Counter component scaffolded. We’ll now add this component to a view in our existing ASP.NET Core Web Application (Existing.Web).

  1. In Solution Explorer, locate the Existing.Web project and expand the Pages folder. Open the Index.cshtml file. This is the default view that displays when the Existing.Web application is run.

The Solution Explorer panel displays with the Existing.Web project expanded along with the Pages folder. The Index.cshtml file is highlighted.

  1. Directly beneath the @page annotation, add the following using statement to give us access to the Counter component:
@using BlazorApp.Pages

A portion of the code window for Index.cshtml displays with the above code highlighted.

  1. Beneath the div element, add the following code to include the Counter component in the view:
<component type="typeof(Counter)" render-mode="WebAssemblyPrerendered" />

A portion of the code window for Index.cshtml displays with the above markup highlighted.

Step 5. Run the Existing Web Application

Run the existing web application project and notice how you can interact with the Counter component from the Blazor WebAssembly project.

  1. In Solution Explorer, right-click the Existing.Web project and select the Set as Startup Project item.

The Solution Explorer displays with the context menu expanded on the Existing.Web project. The Set as Startup Project item is highlighted.

  1. Right-click the Existing.Web project once more and expand the Debug item. Select Start Without Debugging. This will run the existing web application project.

The context menu of the Existing.Web project displays with the Debug item expanded and the Start Without Debugging option selected.

Note: Alternatively, since we set the startup project you can also start an instance (with or without debugging) from the toolbar menu.

  1. In the browser window, press the Counter component’s Click me button multiple times and notice how the counter increases.

The Counter component displays within the existing web application. The Click me button is selected and the counter total is highlighted.

Step 6. Incorporate Telerik UI for Blazor

Now that we’ve successfully used a Blazor WebAssembly component in our existing ASP.NET Core Web Application, we can take advantage of the components provided by Progress Telerik UI for Blazor. This component library is compatible with both Blazor WebAssembly and Blazor Server projects.

  1. Follow the guidance to add Telerik UI for Blazor in the BlazorApp project, follow Steps 2 through 4.

Note: The Server project in our case is the Existing.Web project, and we’ve already added the app.UseStaticFiles() code, so this step can be skipped (Step 4, item #2 in the article linked).

  1. Now we’ll add a Telerik UI for Blazor component to the existing Counter component. In the BlazorApp project, locate and open the Pages/Counter.razor file.
  1. Replace the code in the file with the following and save the file:
@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

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

<br/>

<hr />

<TelerikButton OnClick="@SayHelloHandler" ThemeColor="primary">Say Hello</TelerikButton>

<br />

@helloString


@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }

    MarkupString helloString;

    void SayHelloHandler()
    {
        string msg = string.Format("Hello from <strong>Telerik Blazor</strong> at {0}.<br /> Now you can use C# to write front-end!", DateTime.Now);
        helloString = new MarkupString(msg);
    }
}
  1. Run the application once more, and press the Say Hello button to use the TelerikButton component!

The Counter widget is displayed with the Telerik button component. The message displays Hello from Telerik Blazor with a timestamp and Now you can use C# to write front-end!

Conclusion

In this article, we integrated Blazor components into an existing ASP.NET Core web application. This opens up a less disruptive migration path for moving existing projects to Blazor WebAssembly.

Try Telerik UI for Blazor

Develop new Blazor apps and modernize legacy web projects in half the time with a high-performing Grid and 100+ truly native, easy-to-customize Blazor components to cover any requirement. Try it for free with our 30-day trial and enjoy our industry-leading support.

Try Now

Feel free to share your feedback or questions in the comments section below. Your input makes a difference.


About the Author

Carey Payette

Carey Payette is a Senior Software Engineer with Trillium Innovations (a Solliance partner), an ASPInsider, a Progress Ninja, a Microsoft Certified Trainer and a Microsoft Azure MVP. Her primary focus is cloud integration and deployment for the web, mobile, big data, AI, machine learning and IoT spaces. Always eager to learn, she regularly tinkers with various sensors, microcontrollers, programming languages and frameworks. Carey is also a wife and mom to three fabulous boys.

Related Posts

Comments

Comments are disabled in preview mode.