First Steps with Client-side UI for Blazor

This article explains how to get the Telerik UI for Blazor components in your Client-side (WebAssembly) Blazor project and start using them quickly. The process consists of the following steps:

  1. Set Up a Blazor Project
  2. Add the Telerik Blazor Components to an Existing Project
    1. Add the Telerik NuGet Feed to Visual Studio
    2. Enable the Components in the Project
  3. Add a Telerik Component to a View

If you are familiar with the Telerik NuGet Feed and Blazor in general, you may want to follow the shorter, more technical article with the same information: What You Need. The current article is designed as a step-by-step tutorial from the basics for new users.

Step 0 - Download the Components

A pre-requisite is having access to the Telerik UI for Blazor components. The easiest way to get them to your development machine is to use the Progress Control Panel or to download the automated installer from your telerik.com account.

If you are not a customer, you can download a free, fully functional trial and the same options will apply to you as well.

Alternatively, you can also access the .nupkg files from our private NuGet feed or by creating a local feed from your installation.

Step 1 - Set Up a Blazor Project

Make sure that you have .NET Core 3.1.9 and Visual Studio 2019 installed.

The latest version of Telerik UI for Blazor is 2.17.0 and it supports .NET Core 3.1.9.

For a client-side Blazor App, we recommend the usage of Blazor (ASP.NET Hosted) project. If you have one, go to the Add the Telerik Blazor Components to an Existing Project section below.

There are three ways to create the project:

Create a Project with the Telerik VS Extensions

You can use the Visual Studio Extensions we provide to create the project for you, so that you can start using the Telerik components immediately.

If you use VS Code, you can also use our VS Code Extension to create a Telerik-enabled project.

The rest of this article will explain the manual steps if you want to have a better understanding of the underlying process.

Create a Project with the CLI

The next section shows how to create the Blazor through the Visual Studio interface. If you are not running Visual Studio, you can create the Blazor project from the command prompt - see the dotnet new command and the arguments related to Blazor apps:

The rest of this article shows some steps through the Visual Studio interface, as it is the most common IDE used with Blazor. Where such steps exist, there are links and instructions on performing them yourself (e.g., adding a nuget feed through the CLI).

Create a Project with Visual Studio

To create a project manually, follow these steps:

  1. Open Visual Studio 2019

  2. Create a New Project

  3. Choose Blazor App and click Next. Then, choose a name and location for the project and click Create.

    Create new ASP.NET Core Web Application

  4. Choose the Blazor WebAssembly App project type, select the ASP.NET Core hosted checkbox, and click Create.

    Select Blazor Project Type

Step 2 - Add the Telerik Blazor Components to an Existing Project

Add the Telerik NuGet Feed to Visual Studio

Telerik UI for Blazor is distributed through our private NuGet feed.

If you don't have an active license, start a UI for Blazor trial. The file this will let you download and install the components, and will also let you setup our online NuGet feed automatically - make sure to select the "Set up Telerik NuGet package source" checkbox:

Automatic Telerik NuGet feed installation

If you prefer to do the process yourself, follow the Setup the Telerik Private NuGet Feed article to set it up manually in case you don't have it already.

Once you have added the Telerik NuGet feed, continue with this tutorial.

Enable the Components in the Project

To have the project use the Telerik UI for Blazor components, follow these steps:

  1. If you don't have an active license, start a UI for Blazor trial. The file this will let you download and install the components, and will also let you setup our online NuGet feed automatically - make sure to select the "Set up Telerik NuGet package source" checkbox:

    Automatic Telerik NuGet feed installation

  2. Install the Telerik.UI.for.Blazor NuGet package to your Blazor project:

    You can watch a video tutorial how to add the Telerik components to your project here, or you can follow the text instructions after it.

    1. Right-click on the Client project in the solution and select Manage NuGet Packages:

    2. Choose the telerik.com feed, find the Telerik.UI.for.Blazor package and click Install (make sure to use the latest version). If you don't have a commercial license, you will only see Telerik.UI.for.Blazor.Trial. Use that instead.

      Add Telerik Blazor Package to Client Project

  3. add the telerik-blazor.js file to your main index file - wwwroot/index.html:

    HTML

    <head>
      . . .
      <script src="_content/Telerik.UI.for.Blazor/js/telerik-blazor.js" defer></script>
    
      <!-- For Trial licenses use
        <script src="_content/Telerik.UI.for.Blazor.Trial/js/telerik-blazor.js" defer></script>
      -->
    </head>
    

    To enable the use of static assets in your project, make sure you have the following line to your Server project Startup.cs file:

    C#

    // Startup.cs
    namespace MyBlazorAppName
    {
        public class Startup
        {
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                //more code may be present here
    
                //make sure this is present to enable static files from a package
                app.UseStaticFiles();
    
                //more code may be present here
            }
        }
    }
    
  4. Open the ~/wwwroot/index.html file in the client web application and register the Theme stylesheet:

    <head>
      . . .
        <link rel="stylesheet" href="_content/Telerik.UI.for.Blazor/css/kendo-theme-default/all.css" />
    
        <!-- For Trial licenses use
            <link rel="stylesheet" href="_content/Telerik.UI.for.Blazor.Trial/css/kendo-theme-default/all.css" />
          -->
    </head>
    
  5. Open the ~/Program.cs file in the client web application and register the Telerik Blazor service:

    C#

    using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using System.Threading.Tasks;
    using System.Net.Http;
    using System;
    
    namespace ClientBlazorProject.Client // make sure this matches your actual WASM project namespace
    {
        public class Program
        {
            public static async Task Main(string[] args)
            {
                // sample host builder for a WASM app, yours may differ
                var builder = WebAssemblyHostBuilder.CreateDefault(args);
                builder.RootComponents.Add<App>("app");
                builder.Services.AddSingleton(new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
                // there may be more code here
    
                // register the Telerik services
                builder.Services.AddTelerikBlazor();
    
                // there may be more code here
                // sample host builder for a WASM app, yours may differ
                await builder.Build().RunAsync();
            }
        }
    }
    
  6. Add the following to your ~/_Imports.razor file so the project recognizes our components in all files:

    _Imports.razor

    @using Telerik.Blazor
    @using Telerik.Blazor.Components
    
  7. Open the main layout file (by default, the ~/Shared/MainLayout.razor file in the Blazor project) and add a <TelerikRootComponent> element as its root element - the rest of the content should be inside this new tag. The layout file should look similar to this (there may be additional elements in it in your case to create your own app design):

    MainLayout.razor

    @inherits LayoutComponentBase
    
    <TelerikRootComponent>
    
        <div class="sidebar">
            <NavMenu />
        </div>
    
        <div class="main">
            <div class="top-row px-4">
                <a href="https://docs.microsoft.com/en-us/aspnet/" target="_blank">About</a>
            </div>
    
            <div class="content px-4">
                @Body
            </div>
        </div>
    
    </TelerikRootComponent>
    

Now your project can use the Telerik UI for Blazor components.

Step 3 - Add a Telerik Component to a View

The final step is to actually use a component on a view and run it in the browser. For example:

  1. Add a Button component to the ~/Pages/Index.razor view:

    RAZOR

    <TelerikButton>Say Hello</TelerikButton>
    
  2. Optionally, hook up a click handler that will show a message. The resulting view should look like this:

    RAZOR

    @page "/"
    
    <TelerikButton OnClick="@SayHelloHandler" Primary="true">Say Hello</TelerikButton>
    
    <br />
    
    @helloString
    
    @code {
       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);
       }
    }
    
  3. Run the app in the browser by pressing F5. You should see something like this, after clicking the button:

    App in the browser

Now you have the Telerik components running in your Blazor app.

Next Steps

Next, you can explore the live demos and the rest of the documentation. You can also find the entire demos project in the demos folder of your local installation - it is fully runnable and you can inspect, modify and copy the code from it.

Many applications have a data grid component, and you can get started with the fully featured Telerik Grid in the Grid Overview article.

You can also explore the List of Components and pick the ones you are interested in.

See Also

In this article
Not finding the help you need? Improve this article