A clean, minimal example of TempData usage in Blazor Static Server-Side Rendering (SSR) for the post-redirect-get pattern. Built on the empty Blazor SSR template with no other dependencies.
The solution includes a simple BlazorSsrRedirectManager
service that handles redirecting and saving TempData, a TempDataAccessor
service that retrieves TempData, and a helper StatusMessageDisplay
component for TempData-based status messages. An example Home
form demonstrates their use.
TempData is an alternative to other mechanisms such as query strings and flash cookies, which are less ideal for privacy, security and reliability concerns in many use cases.
While Blazor's official templates don't include built-in TempData support yet, it can be easily added via:
// Program.cs
// Register minimum services to make ITempDataDictionary and default cookie provider available via DI.
// AddRazorPages or AddControllersWithViews also work (especially if you need those in your project)
builder.Services.AddMvcCore().AddViews();
Please consider posting in this Github issue to request official TempData support in Blazor SSR from Microsoft.
// Redirect with status message and TempData
redirectManager.RedirectToWithStatusAndTempData("/profile", "Please complete your profile", Severity.Info,
new Dictionary<string, object?>
{
{ "EmailAddress", email },
{ "UserId", userId }
});
// Access TempData via TempDataAccessor
tempDataAccessor
.TryGet<string?>("EmailAddress", out var email, out bool hasEmail)
.TryGet<Guid?>("UserId", out var userId, out bool hasId)
.Save();
// Note: You can also inject ITempDataDictionaryFactory and access TempData manually,
// as shown in Forecast.razor.cs
- Explicit Save() Required: You must call
Save()
after usingTempDataAccessor
(orITempDataDictionary
). The autoSaveTempDataFilter
isn't triggered likely due to the framework'sNavigationException
mechanism for static SSR (could be fixed in .NET 10). - TempData Restrictions: Subject to the same TempData restrictions as in Razor Pages/MVC. Use simple serializable types supported by the framework (
string
,Enum
,int
,bool
,Guid
,DateTime
, their nullable counterparts, and collections ofint
orstring
). - Size Limit: Subject to cookie size limitations when using the default cookie data provider. You should be able to configure session provider if needed (untested).
To learn more about TempData, see the official ASP.NET Core TempData documentation.
- .NET 8 or .NET 9 Blazor Web App.
Feel free to submit issues, fork, and create pull requests for any improvements.