1

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?

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.