FlashMessage 1.0.2

dotnet add package FlashMessage --version 1.0.2
                    
NuGet\Install-Package FlashMessage -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FlashMessage" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FlashMessage" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="FlashMessage" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FlashMessage --version 1.0.2
                    
#r "nuget: FlashMessage, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=FlashMessage&version=1.0.2
                    
Install FlashMessage as a Cake Addin
#tool nuget:?package=FlashMessage&version=1.0.2
                    
Install FlashMessage as a Cake Tool

About FlashMessage

FlashMessage is a simple container for rendering flash messages.

To use this, create a FlashMessage object, add messages to the object, and then store that in a session. When returning, if there are messages, those can be rendered.

FlashMessage is designed for use with Boostrap 3.4.1+ or Toastr. You must include and activate these dependencies separately. FlashMessage expects them to be there.

Setting Up FlashMessage

Installing

You can grab the package from https://www.nuget.org/packages/FlashMessage/ if you want to install using NuGet in your C# Project.

Update _ViewImports.cshtml for Tag Helper

Your _ViewImports.cshtml file must contain the following for the Razor Page to be able to replace the <flash></flash> tags.

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, FlashMessage

Add Toastr or Bootstrap

After the NuGet Package is installed, make sure you have a reference to either Boostrap 3.4.1+ or Toastr in your HTML depending on which you prefer to use. You must have JQuery available for this to work, and that must be loaded prior to loading Toastr.

The _Layout.cshtml head section should contain the following:

<link href="~/css/toastr.min.css" rel="stylesheet" type="text/css" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/js/toastr.min.js"></script>

You are recommended to have both Toastr and Bootstrap in your project layout pages, so you will have the freedom to use both on your pages.

Register Session in your Program.cs

Using the Session is necessary for FlashMessage. The session will always use the term "FlashMessages" as the key.

Add the service scope for dependency injection while configuring the builder.

builder.Services.AddDistributedMemoryCache(); // Required for session state.
builder.Services.AddRazorPages();
builder.Services.AddSession();
builder.Services.AddHttpContextAccessor(); // Required for FlashMessage to access the session.
builder.Services.AddScoped<IFlash, Flash>();

var app = builder.Build();

In your Program.cs, you must make sure that UseSession is put into place before the routing; otherwise, you will not have access to HttpContext.Session in your Razor Pages in time.

app.UseStaticFiles();
*app.UseSession();*
app.UseRouting();

Set your using statement

Put the appropriate using statement in your code. See the basic examples for actual usage examples.

using FlashMessage;

Simple Example Using FlashMessage with Bootstrap Alerts

Using TagHelper To Render in Razor Pages

Dependency Injection into Razor Page

private readonly ILogger<IndexModel> _logger;
private readonly IFlash _flash;
public string Testing {get;set;} = string.Empty;

public IndexModel(ILogger<IndexModel> logger, IFlash flash)
{
    _logger = logger;
    _flash = flash;
    // In this example, we are using Bootstrap alerts, but you can choose Toastr instead.
    _flash.MessageFlashType = FlashMessage.Enums.FlashType.BootstrapAlert;
}

Set Flash Messages During Errors or Success

public void OnGet()
{
    _flash.Error("This is an error message test.");
    _flash.Success("This is a success message test.");
    _flash.Warning("This is warning message test.");
    _flash.SaveSession();
}

Render All Flashes in Markup

<flash></flash>

Render Flashes of a Specific Type in Markup

<flash type="error"></flash>
<flash type="success"></flash>
<flash type="warning"></flash>
<flash type="info"></flash>
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 425 3/25/2025