Blazor, .Net 8 and Global Interactivity issue with Identity
Asked Answered
C

1

10

I have been playing around with the new .Net 8 Blazor templates and ran into a problem with Identity and setting a global InteractiveServer render mode. The change I made was in the App.Razor file to the :

<Routes @rendermode="InteractiveServer" />

The reason I added this was because I was also attempting to get MudBlazor integrated and this was required for it to run properly (as far as I could tell at least). MS does list the ability to do this via docs as well so appears to be a legitimate choice to make to set this global render mode.

The issue I then faced was with any page using the AccountLayout component e.g. Login, Register, ResetPassword etc because it loops on this part:

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);
}

As it's globally set it's always hit so goes into an endless navigation loop. I attempted to set the account layout and/or account pages to use a rendermode of null, default, static etc but they don't seem to be valid options nor can I find any examples of anyone doing this.

So my question is, does anyone know of a way to either set default/static on a component for render mode OR know of a way to get MudBlazor working in .net 8 project without using global interactive mode? I have obviously tried setting the rendermode to interactive on relevant pages using mudblazor.

Courbevoie answered 4/12, 2023 at 11:54 Comment(0)
C
12

I have found that this is currently an issue that appears to be getting fixed by MS. Within the issues I did find a workaround for now within this raised issue: https://github.com/dotnet/aspnetcore/issues/51476

Essentially changing the rendermode globally in app.razor based on the url:

<body>
    <Routes @rendermode="RenderModeForPage" />
    <script src="_framework/blazor.web.js"></script>
    <script src="_content/MudBlazor/MudBlazor.min.js"></script>
</body>

@code {
    [CascadingParameter]
    private HttpContext HttpContext { get; set; } = default!;

    private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account") ? null : InteractiveServer;
}

Can confirm I have tried this and have Identity pages and MudBlazor working on other pages as originally intended. At least it appears future templates should already contain the resolution for this.

Courbevoie answered 4/12, 2023 at 12:12 Comment(1)
Thanks a lot! I as a Blazor Noob lost so many hours on this! Saves my project. Really appreciate.Flogging

© 2022 - 2024 — McMap. All rights reserved.