admin管理员组

文章数量:1122832

I have a page with an OnInitialized() method. In that method, I set some cookies and get information from the database include a route to redirect to. I then try to use the NavigationManager's NavigateTo() method to navigate to that URL, but Visual Studio throws an error with not much info.

    Message "Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown."   string

Visual Studio allows me to continue and the app will redirect, if it is not the same as the current URL, but my Login component is not updated. I have to refresh the page manually to see the page as it should be.

This behavior seems odd. Why do I get the exception? And why is the other component not refreshed after changing locations?

I have a page with an OnInitialized() method. In that method, I set some cookies and get information from the database include a route to redirect to. I then try to use the NavigationManager's NavigateTo() method to navigate to that URL, but Visual Studio throws an error with not much info.

    Message "Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown."   string

Visual Studio allows me to continue and the app will redirect, if it is not the same as the current URL, but my Login component is not updated. I have to refresh the page manually to see the page as it should be.

This behavior seems odd. Why do I get the exception? And why is the other component not refreshed after changing locations?

Share Improve this question edited Dec 2, 2024 at 6:51 Zhi Lv 21.2k1 gold badge26 silver badges36 bronze badges asked Nov 22, 2024 at 21:38 esr124esr124 514 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This is a known issue with SSR, the proposed solution is to set your IDE to ignore this specific exception.

https://github.com/dotnet/aspnetcore/issues/53996

Someone has suggested the following workaround:

public static class BlazorSsrRedirectManagerExtensions
{
    public static void RedirectTo(this HttpContext httpContext, string redirectionUrl)
    {
        ArgumentNullException.ThrowIfNull(httpContext);

        httpContext.Response.Headers.Append("blazor-enhanced-nav-redirect-location", redirectionUrl);
        httpContext.Response.StatusCode = 200;
    }
}

add this to the calling page:

@code{
    [CascadingParameter] public HttpContext? HttpContext { get; set; }
}

本文标签: Issue redirecting with NavigationManager in OnInitialized method for Blazor appStack Overflow