Getting Started with Blazor Smart Rich Text Editor in Blazor Web App

This section explains how to add the Blazor Smart Rich Text Editor component in your Blazor Web App using Visual Studio, Visual Studio Code, and the .NET CLI.

Create a new Blazor Web App

Create a Blazor Web App using Visual Studio via Microsoft Templates or the Syncfusion® Blazor Extension. For detailed instructions, refer to the Blazor Web App Getting Started documentation.

Run the following command to create a new Blazor Web App.

dotnet new blazor -o BlazorWebApp --interactivity Auto
cd BlazorWebApp
cd BlazorWebApp.Client

Alternatively, create a Blazor Web App using Visual Studio Code via Microsoft Templates or the Syncfusion® Blazor Extension, or the C# Dev Kit extension.

Run the following command to create a new Blazor Web App.

dotnet new blazor -o BlazorWebApp --interactivity Auto
cd BlazorWebApp
cd BlazorWebApp.Client

Configure the appropriate Interactive render mode and Interactivity location while creating a Blazor Web App. For detailed information, refer to the interactive render mode documentation.

Install the required Blazor packages

Install the Syncfusion.Blazor.SmartRichTextEditor and Syncfusion.Blazor.Themes NuGet packages. All Syncfusion Blazor packages are available on nuget.org. See the NuGet packages topic for details. If using the WebAssembly or Auto render modes in the Blazor Web App, install these packages in the .Client project.

  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Syncfusion.Blazor.SmartRichTextEditor and Syncfusion.Blazor.Themes) and install them.

Alternatively, you can install the same packages using the Package Manager Console with the following commands.

Install-Package Syncfusion.Blazor.SmartRichTextEditor -Version 34.2.2
Install-Package Syncfusion.Blazor.Themes -Version 34.2.2

Open the terminal and run the following commands.

dotnet add package Syncfusion.Blazor.SmartRichTextEditor -v 34.2.2
dotnet add package Syncfusion.Blazor.Themes -v 34.2.2

Open the command prompt and run the following commands.

dotnet add package Syncfusion.Blazor.SmartRichTextEditor -v 34.2.2
dotnet add package Syncfusion.Blazor.Themes -v 34.2.2

Add import namespaces

After the packages are installed, open the ~/_Imports.razor file in the .Client project and import the Syncfusion.Blazor and Syncfusion.Blazor.SmartRichTextEditor namespaces.

@using Syncfusion.Blazor
@using Syncfusion.Blazor.SmartRichTextEditor

Register the Blazor service

Open the Program.cs file in Blazor Web App and register the Blazor service and include the required namespace reference using Syncfusion.Blazor; at the top. If the Interactive Render Mode is set to WebAssembly or Auto, register the Blazor service in Program.cs files of both the server and client projects in your Blazor Web App.

builder.Services.AddSyncfusionBlazor();

Configure AI Service

Use the following instructions to register and configure an AI model for your application.

OpenAI

Install the following NuGet packages to your project:

You can install these packages using different methods as shown below:

  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Microsoft.Extensions.AI and Microsoft.Extensions.AI.OpenAI) and install them.

Alternatively, you can install the same packages using the Package Manager Console with the following commands.

Install-Package Microsoft.Extensions.AI
Install-Package Microsoft.Extensions.AI.OpenAI

Open the terminal and run the following commands.

dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI

Open the command prompt and run the following commands.

dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI
  • For OpenAI, obtain an API key from OpenAI and specify the desired model (e.g., gpt-3.5-turbo, gpt-4). Store the API key securely in a configuration file.

  • To configure the AI service, add the following settings to the Program.cs file in your Blazor Web app.

using Syncfusion.Blazor;
using Syncfusion.Blazor.AI;
using Microsoft.Extensions.AI;
using OpenAI;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

builder.Services.AddSyncfusionBlazor();

string openAIApiKey = "API-KEY";
string openAIModel = "OPENAI_MODEL";
OpenAIClient openAIClient = new OpenAIClient(openAIApiKey);
IChatClient openAIChatClient = openAIClient.GetChatClient(openAIModel).AsIChatClient();
builder.Services.AddChatClient(openAIChatClient);

builder.Services.AddSingleton<IChatInferenceService, SyncfusionAIService>();

var app = builder.Build();

// ... rest of configuration

Azure OpenAI

Install the following NuGet packages to your project:

You can install these packages using different methods as shown below:

  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Microsoft.Extensions.AI, Microsoft.Extensions.AI.OpenAI and Azure.AI.OpenAI) and install them.

Alternatively, you can install the same packages using the Package Manager Console with the following commands.

Install-Package Microsoft.Extensions.AI
Install-Package Microsoft.Extensions.AI.OpenAI
Install-Package Azure.AI.OpenAI

Open the terminal and run the following commands.

dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI
dotnet add package Azure.AI.OpenAI

Open the command prompt and run the following commands.

dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI
dotnet add package Azure.AI.OpenAI
  • For Azure OpenAI, first deploy an Azure OpenAI Service resource and model, then values for azureOpenAIKey, azureOpenAIEndpoint and azureOpenAIModel will all be provided to you.

  • To configure the AI service, add the following settings to the Program.cs file in your Blazor Web app.

using Syncfusion.Blazor;
using Syncfusion.Blazor.AI;
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using System.ClientModel;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

builder.Services.AddSyncfusionBlazor();

string azureOpenAIKey = "AZURE_OPENAI_KEY";
string azureOpenAIEndpoint = "AZURE_OPENAI_ENDPOINT";
string azureOpenAIModel = "AZURE_OPENAI_MODEL";
AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient(
     new Uri(azureOpenAIEndpoint),
     new ApiKeyCredential(azureOpenAIKey)
);
IChatClient azureOpenAIChatClient = azureOpenAIClient.GetChatClient(azureOpenAIModel).AsIChatClient();
builder.Services.AddChatClient(azureOpenAIChatClient);

builder.Services.AddSingleton<IChatInferenceService, SyncfusionAIService>();

var app = builder.Build();

// ... rest of configuration

To configure other AI services, refer to the following sections Ollama AI Service and Custom AI Service.

Add stylesheet and script resources

The theme stylesheet and script can be accessed from NuGet through Static Web Assets. Include the stylesheet at the end of the <head> section in the App.razor file.

<link href="_content/Syncfusion.Blazor.Themes/fluent2.css" rel="stylesheet" />

Include the required script references at the end of the <body> section in the App.razor file to enable Smart Rich Text Editor functionality.

<script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>

Add Blazor Smart Rich Text Editor component

Open a Razor file located in the ~/Pages/*.razor (for example, Home.razor) and add the Blazor Smart Rich Text Editor component inside the .Client project razor file.

If the interactivity location is set to Per page/component in the Web App, define a render mode at the top of the razor file. (For example, InteractiveServer, InteractiveWebAssembly or InteractiveAuto). If the Interactivity is set to Global with Auto or WebAssembly, the render mode is automatically configured in the App.razor file by default.

@rendermode InteractiveAuto

@using Syncfusion.Blazor.SmartRichTextEditor

<SfSmartRichTextEditor>
    <h2>Welcome to Smart Rich Text Editor</h2>
    <p>This editor showcases AI-assisted content enhancement. Try selecting text and utilizing the AI tools to refine your writing.</p>
    <p>Use the <strong>Smart Action</strong> dropdown toolbar to summarize, expand, or adjust the tone. Press <kbd>Alt</kbd>+<kbd>Enter</kbd> to open the AI Query dialog.</p>
    <ul>
       <li><strong>Purpose:</strong> Create clear and concise documentation snippets.</li>
       <li><strong>Tone:</strong> Professional yet approachable.</li>
       <li><strong>Example:</strong> Transform this paragraph into bullet points using AI.</li>
    </ul>
    <AssistViewSettings Placeholder="Start typing or use AI assistance to enhance your content..." />
</SfSmartRichTextEditor>

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The Blazor Smart Rich Text Editor component will render in your default web browser.

Open the terminal and navigate to the main project folder (for example, BlazorWebApp) and run the following command.

cd BlazorWebApp
dotnet run

Open the command prompt and navigate to the main project folder (for example, BlazorWebApp) and run the following command.

cd BlazorWebApp
dotnet run
  • Start typing content and use the AI tools in the toolbar to enhance your editing experience.

  • Use Alt+Enter to open the AI Query dialog for content improvement.

Syncfusion Smart Rich Text Editor - Output

View Sample in GitHub.

See also

  1. Getting Started with Blazor Smart Rich Text Editor in Server App
  2. Rich Text Editor Documentation