Search code examples
blazornavigationblazor-server-side.net-9.0

Blazor server upgraded from 8 to 9, NavigationManager.NavigateTo() exception when going from static ssr page to interactive page


I upgraded blazor server 8 project to 9, the identity pages use static ssr mode and have a IdentityRedirectManager class with this method:

[DoesNotReturn]
    public async void RedirectTo(string? uri)
     {
         uri ??= "";
    
         // Prevent open redirects.
         if (!Uri.IsWellFormedUriString(uri, UriKind.Relative))
         {
             uri = navigationManager.ToBaseRelativePath(uri);
         }
    
         // During static rendering, NavigateTo throws a NavigationException which is handled by the framework as a redirect.
         // So as long as this is called from a statically rendered Identity component, the InvalidOperationException is never thrown.
         await Task.Delay(100);
         navigationManager.NavigateTo(uri); // << error here that prevents page navigation
         
         throw new InvalidOperationException($"{nameof(IdentityRedirectManager)} can only be used during static rendering.");
     }

when I login and login page (a static ssr page) wants to redirect to homepage (a page with -new InteractiveServerRenderMode(prerender: false)-), an exception is thrown, and site crashes. I put await Task.Delay(100); before navigationManager.NavigateTo(uri); still it dosn't work.

all layouts and pages that use static ssr mode, are decorated with:

@attribute [ExcludeFromInteractiveRouting]

and the render modes in App.razor is like this:

private IComponentRenderMode PageRenderMode => HttpContext.AcceptsInteractiveRouting() ? new InteractiveServerRenderMode(prerender: false) : null;

while this site works in .net 8, but upgrade to .net 9, gets error.

Is there any way to correct this?


Solution

  • Finally it worked, in the LoginDisplay the currenturl was:

    currentUrl = uriHelper.ToBaseRelativePath(uriHelper.Uri)
    

    I changed it to:

    currentUrl = System.Web.HttpUtility
        .UrlEncode($"/{uriHelper.ToBaseRelativePath(uriHelper.Uri)}");
    

    The beggining slash $$"/{..}", helped to resolve the issue.