Toast Notifications In Blazor

In this article, we will learn how we can display notifications in blazor. We will use Blazored.Toast package to popup notification.

If you are new to blazor or follow steps to create a blazor app from here. I assume you have already created a blazor server app. Now you need to follow the below steps.

Install Blazored.Toast, to install this you need to execute below command in Package Manager Console or find Blazored.Toast in Nuget-Solution.

Install-Package Blazored.Toast

you also can install Blazored.Toast using Dotnet CLI

dotnet add package Blazored.Toast

Once you have installed Blazored.Toast package, you need to do the following steps to use Toast notification.

Register its service in Startup class. Which is available in your blazor server application.

To Register service, you need to include the following namespace.

using Blazored.Toast;

Next, register the service in the ConfigureServices() method of Startup class.

services.AddBlazoredToast();

Import following lines inside _Imports.razor file. This will allow us to use Blazored.Toast in each component without importing directive for each component.

@using Blazored.Toast
@using Blazored.Toast.Services

Add the following CSS links inside _Host.cshtml file.

<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

<link href="_content/Blazored.Toast/blazored-toast.css" rel="stylesheet" />

paste the following content where you want to use Toast notification, I’ll use an Index.razor component.

@page "/"

@inject IToastService toastService

<button class="btn btn-info" @onclick="@(() => toastService.ShowInfo("Welcome to TheCodehubs"))">Info Toast</button>
<button class="btn btn-success" @onclick="@(() => toastService.ShowSuccess("You are sign up successfully!"))">Success Toast</button>
<button class="btn btn-warning" @onclick="@(() => toastService.ShowWarning("You are about to leave website!"))">Warning Toast</button>
<button class="btn btn-danger" @onclick="@(() => toastService.ShowError("Oops! Error has occured"))">Error Toast</button>

In MainLayout.razor file, put below tag at end of the file.

<BlazoredToasts />

Or if you wanna customize you can use the below code and replace it with the above tag.

<BlazoredToasts Position="ToastPosition.TopRight"
                Timeout="10"
                SuccessClass="success-toast-override"
                SuccessIconClass="fa fa-check"
                ErrorIconClass="fa fa-times" 
                InfoIconClass="fa fa-info"
                WarningIconClass="fa fa-warning"/>

And That’s it, now you can use Toast notification. when you run the application and following output will display in a browser.

Recommend articles which you should read once.

  1. CRUD Using Blazor, Entity Framework Core And Dapper
  2. Sorting Table In Blazor
  3. Pagination In Blazor
  4. Searching Feature In Blazor
  5. Validation In Blazor Application
  6. Deploy A Blazor Application On IIS
  7. Use LocalStorage In Blazor
  8. Modal Popup In Blazor

hope you guys found something useful. Please give your valuable feedback/comments/questions about this article. Please let me know how you like and understand this article and how I could improve it.

6 Comments

  1. Muneer

    Hi,

    I have implemented the same, but my toast messages disappears immediately.
    What could be the reason

    0
    0
    Reply
    1. Faisal Pathan

      try to increase timeout

      0
      0
      Reply
      1. Muneer

        Thanks for ur quick reply Faisal,

        I did that, but still not working… It seems the complete page is getting refreshed… I don’t know why.
        As I am new to Blazor, so not able to find out the issue. I would be thankful if u help me out.

        0
        0
        Reply
  2. Alex A

    Note I had an issue with this working , but needed IconType=”IconType.FontAwesome” in the MainLayout.razor such as:

    0
    0
    Reply
  3. Faisal Pathan

    Best reply I can give now is, review each step again. I’m quite sure you missed minor thing.

    0
    0
    Reply
  4. Tone

    Hi,

    Very nice idea. I am new to Blazor and come from the WPF world. I have setup my page/component similar to a WPF code behind, where all my c# is in one file:

    public class ListsPageBase : ComponentBase
    {
    public ToastService toastService { get; set; } = new ToastService();

    public void OnSaveRecordClick(MouseEventArgs ars)
    {
    try
    {
    api.ListTypeWrite(Guid.Parse(projectIdParameter), projectsListTypeList);

    toastService.ShowSuccess(“You are sign up successfully!”);
    }
    catch (Exception ex)
    {
    ErrorDialogText = ex.Message;
    IsErrorDialogVisible = true;
    }
    }

    }

    I have a button on a Toolbar and when the user clicks this button I want your Toast notification to display. I followed your instructions as above but cannot get it to post.

    In my .razor file at the bottom I did put as you stated.

    I’m not sure what I do wrong.

    Could you please show me?

    Thank you kindly.

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories