Getting Started with Blazor Smart Rich Text Editor in Server App

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

Create a new Blazor Server App

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

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

dotnet new blazor -o BlazorApp --interactivity Server
cd BlazorApp

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

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

dotnet new blazor -o BlazorApp --interactivity Server
cd BlazorApp

Configure the appropriate Interactive render mode and Interactivity location while creating a Blazor Server 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.

  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 35.1.37
Install-Package Syncfusion.Blazor.Themes -Version 35.1.37

Open the terminal and run the following commands.

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

Open the command prompt and run the following commands.

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

Add import namespaces

After the packages are installed, open the ~/Components/_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 Server App and register the Blazor service and include the required namespace reference using Syncfusion.Blazor; at the top.

builder.Services.AddSyncfusionBlazor();

Configure AI Service

Follow the instructions below to register an AI model in 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 Server app.

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

var builder = WebApplication.CreateBuilder(args);

....

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();
....

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 Server app.

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

var builder = WebApplication.CreateBuilder(args);

....

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();
....

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 ~/Components/Pages/*.razor (for example, Home.razor) and add the Blazor Smart Rich Text Editor component inside the razor file.

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

@rendermode InteractiveServer

@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 Blazor Smart Rich Text Editor component will render in your default web browser.

Open the terminal and run the following command.

dotnet run

Open the command prompt and run the following command.

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 Web App
  2. Rich Text Editor Documentation