Implementing Dynamic Bootstrap Toasts in ASP.NET Core with JSON Configuration

By

Bootstrap toasts are lightweight, non‑intrusive notifications that mimic the push alerts found on mobile and desktop systems. By combining Bootstrap's toast component with C# server‑side code, you can create flexible, data‑driven toast messages that adapt to different scenarios—such as error alerts, success confirmations, or informational messages. This guide walks you through reading toast options from an appsettings.json file, mapping them to C# models, injecting the configuration via dependency injection, and displaying the toast on a Razor page with a simple button click.

1. What is a Bootstrap toast and how does it integrate with ASP.NET Core?

A Bootstrap toast is a small notification box that appears temporarily on screen, similar to browser or OS push notifications. In ASP.NET Core, you can dynamically feed toast data (title, message, delay) from C# code into the Bootstrap HTML. This integration allows you to control toast content based on server logic—for example, showing a database error message only when a connection fails. The toast is rendered on a Razor page using model properties, and its behavior (such as auto‑dismiss time) is set via data-bs-delay attributes. This approach gives you full flexibility to tailor notifications without hard‑coding them in the front end.

Implementing Dynamic Bootstrap Toasts in ASP.NET Core with JSON Configuration
Source: dev.to

2. How do you configure toast options using appsettings.json?

Toast options are stored in a dedicated section of the appsettings.json file, making them easily changeable without code modifications. For example, you can define a ToastOptions section with a sub‑object like DatabaseError containing ToastMessage, ToastTitle, and ToastDelay. This structure allows you to add multiple toast configurations (e.g., SuccessNotification, ValidationWarning) by simply adding new objects under ToastOptions. The JSON values are later parsed into C# objects and injected into your application at startup.

3. What C# model classes are needed to parse the toast JSON configuration?

You need two classes: ToastOptions and ToastMessageOptions. ToastOptions contains a property for each toast scenario—for instance, public ToastMessageOptions DatabaseError { get; set; } = new();. The ToastMessageOptions class defines three string properties: ToastMessage, ToastTitle, and an integer ToastDelay. These models map directly to the JSON structure. By initializing the property with new(), you avoid null references when no configuration is provided.

4. How do you register the toast configuration in Program.cs using dependency injection?

In Program.cs, you first add services like builder.Services.AddRazorPages(). Then you bind the ToastOptions section from the configuration to the ToastOptions class using builder.Services.Configure<ToastOptions>(builder.Configuration.GetSection(nameof(ToastOptions))). After that, you register a service (e.g., ReadToastConfiguration) that will read these options. This service can be injected into your page models. Using AddScoped ensures a fresh instance per request, which is ideal for reading settings that may vary per user session.

Implementing Dynamic Bootstrap Toasts in ASP.NET Core with JSON Configuration
Source: dev.to

5. How can a Razor page trigger a toast via a button press?

On the Razor page, place a form with asp-page-handler="ShowToast". This points to a public IActionResult OnPostShowToast() method in the page model. When the user clicks the submit button, a POST request is sent to that handler. Inside the handler, you can set model properties—such as ToastMessage, ToastTitle, and ToastDelay—by reading them from the injected IOptions<ToastOptions> service. The page then renders the toast HTML conditionally when these properties are non‑empty. This separates the triggering logic from the display logic.

6. How do you display the toast in the Razor view using model properties?

At the bottom of the Razor page, include a conditional block that checks if Model.ToastMessage is not null or whitespace. Inside it, create a div with Bootstrap toast markup: a container with position-fixed bottom-0 end-0 p-3, and a toast div with classes like toast text-bg-danger border-0. Set data-bs-delay to @Model.ToastDelay and display the title and message from the model. The toast is hidden by default unless server‑side code sets the model values. This conditional rendering ensures the toast only appears when intended.

7. What is the role of the ReadToastConfiguration service?

The ReadToastConfiguration service is an optional helper that encapsulates the logic to fetch toast options from the injected IOptions<ToastOptions> object. It can provide methods like GetDatabaseErrorOptions() that return the appropriate ToastMessageOptions instance. This makes the page model cleaner and more testable. You register it in Program.cs with builder.Services.AddScoped<ReadToastConfiguration>() so that each request gets a fresh service that reads the latest configuration values. Without this service, you would have to duplicate the IOptions usage in every page model.

Tags:

Related Articles

Recommended

Discover More

Transforming Your PS5 into a Linux Gaming PC: Everything You Need to Know10 Key Insights Into Open-Source Documentaries: The Stories Behind the CodeMastering the Dreame FP10: A Pet Owner's Guide to a Self-Cleaning Air PurifierCanvas Breach Exposes Education's Cybersecurity Crisis: Key Questions AnsweredNew Horror Sensation Revives 83-Year-Old Suspense Technique That Has Audiences Gripped