Forum Discussion
Gavin-Williams
Feb 26, 2023Brass Contributor
Convert the standard Blazor navigation menu to a collapsible icon menu
While I admittedly love Blazor I’ve always changed the out-of-the-box navigation menu that comes with it. It’s the first manoeuvre I pull when spinning up a new Blazor app, stripping out the purpl...
H3ALY
Copper Contributor
The issue seems to be more with Identity, the default .NET8 WebApp configuration for a interactive blazor server will set the render mode in the HeadOutlet as:
<HeadOutlet @rendermode="InteractiveServer" />
with the following route:
<Routes @rendermode="InteractiveServer" />
The issue you're seeing is because Identity is expecting the HttpContext to not be null.
AccountLayout.Razor
@if (HttpContext is null)
{
<p>Loading...</p>
}
else
{
@Body
}
@code {
[CascadingParameter]
private HttpContext? HttpContext { get; set; }
protected override void OnParametersSet()
{
if (HttpContext is null)
{
// If this code runs, we're currently rendering in interactive mode, so there is no HttpContext.
// The identity pages need to set cookies, so they require an HttpContext. To achieve this we
// must transition back from interactive mode to a server-rendered page.
NavigationManager.Refresh(forceReload: true);
}
}
}
This is raised here -Update Identity Components in Blazor project template A work around was provided in this issue - Make RenderTreeBuilder.AddComponentRenderMode's renderMode parameter nullable
You can update App.Razor with:
<HeadOutlet @rendermode="@RenderModeForPage" />
<Routes @rendermode="@RenderModeForPage" />
also including the following code block:
@code{
[CascadingParameter] HttpContext HttpContext { get; set; } = default!;
IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
? null
: RenderMode.InteractiveServer;
}
Which I've tested and seems to work, however the tests aren't extensive to say the least.