Getting Started with Smart Paste Button in Blazor Web App

29 Jul 202615 minutes to read

This section explains how to integrate the Blazor Smart Paste Button component in a Blazor Web App using Visual Studio, Visual Studio Code, and the .NET CLI. The Smart Paste Button leverages AI to intelligently populate form fields from copied text, streamlining user input workflows.

To get started quickly with the Blazor Smart Paste Button component, watch this video:

Create a new Blazor Web App

Create a Blazor Web App using Visual Studio via Microsoft Templates or the Blazor Extension.

Run the following command to create a new Blazor Web App with the Interactive Server render mode

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

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

Run the following command to create a new Blazor Web App with the Interactive Server render mode

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

NOTE

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

Open the terminal and run the following commands.

dotnet add package Syncfusion.Blazor.SmartComponents -v 34.1.29
dotnet add package Syncfusion.Blazor.Themes -v 34.1.29

Open the command prompt and run the following commands.

dotnet add package Syncfusion.Blazor.SmartComponents -v 34.1.29
dotnet add package Syncfusion.Blazor.Themes -v 34.1.29

Add import namespaces

After the packages are installed, open the ~/Components/_Imports.razor file and import the Syncfusion.Blazor and Syncfusion.Blazor.SmartComponents namespaces.

@using Syncfusion.Blazor
@using Syncfusion.Blazor.SmartComponents

Register the Blazor service

Open the Program.cs file in Blazor Web App and register the Blazor service.

....
using Syncfusion.Blazor;
....
builder.Services.AddSyncfusionBlazor();
....

Configure AI Service

The Smart Paste Button requires an AI service (OpenAI, Azure OpenAI, or Ollama) to analyze and map copied text to form fields. Follow the instructions below to configure an AI model in your application.

OpenAI Configuration

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 or environment variable.

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
  • To configure the AI service, add the following settings to the Program.cs file in your Blazor Web app.
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Syncfusion.Blazor;
using Syncfusion.Blazor.SmartComponents;
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.AddSyncfusionSmartComponents()
    .InjectOpenAIInference();

var app = builder.Build();
....

Azure OpenAI Configuration

For Azure OpenAI, deploy a resource and model as described in Azure OpenAI documentation. Obtain the API key, endpoint, and model name from your Azure portal.

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
  • To configure the AI service, add the following settings to the Program.cs file in your Blazor Web app.
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Syncfusion.Blazor;
using Syncfusion.Blazor.SmartComponents;
using Syncfusion.Blazor.AI;
using Microsoft.Extensions.AI;
using Azure.AI.OpenAI;
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.AddSyncfusionSmartComponents()
    .InjectOpenAIInference();

var app = builder.Build();
....

NOTE

For versions 28.2.33 to 30.2.6, the Azure.AI.OpenAI package is not included in the SmartComponents dependency. Install the Azure.AI.OpenAI package separately. Refer to Syncfusion version history for the latest dependency information.

Ollama Configuration

To use Ollama for self-hosted models:

  1. Download and install Ollama from Ollama’s official website.
  2. Install a model from the Ollama Library (e.g., llama2:13b, mistral:7b).
  3. Configure the endpoint URL (e.g., http://localhost:11434) and model name.
  • Install the following NuGet packages to your project:
  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Microsoft.Extensions.AI and OllamaSharp) 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 OllamaSharp

Open the terminal and run the following commands.

dotnet add package Microsoft.Extensions.AI
dotnet add package OllamaSharp

Open the command prompt and run the following commands.

dotnet add package Microsoft.Extensions.AI
dotnet add package OllamaSharp
  • To configure the AI service, add the following settings to the Program.cs file in your Blazor Web app.
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Syncfusion.Blazor;
using Syncfusion.Blazor.SmartComponents;
using Syncfusion.Blazor.AI;
using Microsoft.Extensions.AI;
using OllamaSharp;

var builder = WebApplication.CreateBuilder(args);
....

builder.Services.AddSyncfusionBlazor();

string ModelName = "MODEL_NAME";
IChatClient chatClient = new OllamaApiClient("http://localhost:11434", ModelName);
builder.Services.AddChatClient(chatClient);

builder.Services.AddSyncfusionSmartComponents()
    .InjectOpenAIInference();

var app = builder.Build();
....

Add stylesheet and script resources

The theme stylesheet and script can be accessed from NuGet through Static Web Assets. Include the stylesheet and script references in the ~/Components/App.razor file.

...
<link href="_content/Syncfusion.Blazor.Themes/fluent2.css" rel="stylesheet" />
...
<script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>

Add Blazor Smart Paste Button Component

Open a Razor file located in the ~/Components/Pages/*.razor (for example, Home.razor) and add the Blazor Smart Paste Button component inside the razor file. This example uses the Blazor DataForm component to manage form input fields. Install the Syncfusion.Blazor.DataForm package.

NOTE

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 InteractiveAuto

@using Syncfusion.Blazor.DataForm
@using System.ComponentModel.DataAnnotations
@using Syncfusion.Blazor.SmartComponents

<SfDataForm ID="MyForm"
            Model="@EventRegistrationModel">
    <FormValidator>
        <DataAnnotationsValidator></DataAnnotationsValidator>
    </FormValidator>
    <FormItems>
        <FormItem Field="@nameof(EventRegistration.Name)" ID="firstname"></FormItem>
        <FormItem Field="@nameof(EventRegistration.Email)" ID="email"></FormItem>
        <FormItem Field="@nameof(EventRegistration.Phone)" ID="phonenumber"></FormItem>
        <FormItem Field="@nameof(EventRegistration.Address)" ID="address"></FormItem>
    </FormItems>
    <FormButtons>
        <SfSmartPasteButton IsPrimary="true" Content="Smart Paste" IconCss="e-icons e-paste">
        </SfSmartPasteButton>
    </FormButtons>
</SfDataForm>

<br>
<h4 style="text-align:center;">Sample content</h4>
<div>
    Hi, my name is Jane Smith. You can reach me at [email protected] or call me at +1-555-987-6543. I live at 789 Pine Avenue, Suite 12, Los Angeles, CA 90001.
</div>

@code {
    private EventRegistration EventRegistrationModel = new EventRegistration();

    public class EventRegistration
    {
        [Required(ErrorMessage = "Please enter your name.")]
        [Display(Name = "Name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter your email address.")]
        [Display(Name = "Email ID")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Please enter your mobile number.")]
        [Display(Name = "Phone Number")]
        public string Phone { get; set; }

        [Required(ErrorMessage = "Please enter your address.")]
        [Display(Name = "Address")]
        public string Address { get; set; }
    }
}

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The Blazor Smart Paste Button 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

Copy the Sample Content and click the Smart Paste button to see the form fields automatically populated.

Syncfusion Smart Paste Button filling a form in a Blazor Web App

NOTE

View Sample in GitHub.

Performance Considerations

For optimal performance with the Smart Paste Button:

  • Use lightweight AI models (e.g., gpt-3.5-turbo or mistral:7b) for faster processing.
  • Limit form complexity to reduce AI parsing time, especially for large datasets.
  • Cache AI model responses where possible to minimize repeated API calls.

Troubleshooting Common Issues

  • AI Service Configuration Errors: Verify the API key, endpoint, and model name in Program.cs. Check for typos or incorrect values.
  • Network Failures: Ensure a stable internet connection for OpenAI or Azure OpenAI. For Ollama, confirm the local server is running at the specified endpoint (e.g., http://localhost:11434).
  • Form Not Populating: Confirm that the copied text matches the form field structure and that the AI model is correctly configured.

See also

  1. Getting Started with Blazor Web App
  2. Getting Started with Blazor Server App