Skip to content

OnLocationChanged not fired in .NET 8 Preview 7 Blazor Web App #49950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
KeldJ opened this issue Aug 9, 2023 · 5 comments
Closed
1 task done

OnLocationChanged not fired in .NET 8 Preview 7 Blazor Web App #49950

KeldJ opened this issue Aug 9, 2023 · 5 comments
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Milestone

Comments

@KeldJ
Copy link

KeldJ commented Aug 9, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In the code below (https://github.com/KeldJ/BlazorApp9) navigation to all urls are Catched and should fire OnLocationChanged which increases ID.

@page "/{*pageRoute}"
@Attribute [RenderModeServer(false)]
@Inject NavigationManager NavigationManager
@implements IDisposable
Home

@ID.ToString()


PageRoute: @PageRoute

@code {

private int ID { get; set; } = 0;

[Parameter]
public string? PageRoute { get; set; }

protected override async Task OnInitializedAsync()
{
	await Task.Delay(1);
	NavigationManager.LocationChanged += OnLocationChanged;
}

private async void OnLocationChanged(object? sender, LocationChangedEventArgs e)
{
	ID++;
	await InvokeAsync(StateHasChanged);
}

public void Dispose()
{
	NavigationManager.LocationChanged -= OnLocationChanged;
}

}

Expected Behavior

OnLocationChanged should be fired and ID should be increased.

Steps To Reproduce

https://github.com/KeldJ/BlazorApp9

Exceptions (if any)

No response

.NET Version

8.0.100-preview.7.23376.3

Anything else?

No response

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Aug 9, 2023
@bub-bl
Copy link

bub-bl commented Aug 9, 2023

When you set a new uri from the address bar, you lose the state of your app. This is the reason why the LocationChanged event is never triggered and the Id property stay at 0. If you add the following code, you can see, the Id property is now updated when you navigate from the NavigateTo() function.

@page "/{*pageRoute}"
@attribute [RenderModeServer(false)]
@inject NavigationManager NavigationManager
@implements IDisposable

@Id.ToString()
<br/>
PageRoute: @PageRoute

<button @onclick="@(() => NavigateTo("/counter1/1"))">Counter 1</button>
<button @onclick="@(() => NavigateTo("/counter1/2"))">Counter 2</button>
<button @onclick="@(() => NavigateTo("/counter1/3"))">Counter 3</button>

@code {

    private int Id { get; set; }

    [Parameter]
    public string? PageRoute { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await Task.Delay(1);
        NavigationManager.LocationChanged += OnLocationChanged;
    }

    private async void OnLocationChanged(object? sender, LocationChangedEventArgs e)
    {
        Id++;
        await InvokeAsync(StateHasChanged);
    }

    private void NavigateTo(string nav)
    {
        NavigationManager.NavigateTo(nav);
    }
    
    public void Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
    }

}

@KeldJ
Copy link
Author

KeldJ commented Aug 9, 2023

in .NET 8 Preview 6 the OnLocationChanged get fired using regular a href links like the attached Git Project.

The project on https://cms1.l-mediehus.dk/ (Early alfa version) are built on Preview 6 and the links are catched by OnLocationChanged as desired

@bub-bl
Copy link

bub-bl commented Aug 9, 2023

It seems to be a bug. But yes, NavigationManager seems to have a problem with LocationChanged. It's never triggered. I've tried putting them in App.razor and MainLayout.razor but I always get the same result. LocationChanged is only triggered in the page where it was registered and when we use NavigationManager.NavigateTo() to change the location.

We also get the same result with RenderModeWebAssembly.

@mkArtakMSFT mkArtakMSFT added this to the 8.0-rc2 milestone Aug 9, 2023
@mkArtakMSFT
Copy link
Contributor

Related and will be partially addressed as part of #48766

@mkArtakMSFT mkArtakMSFT added bug This issue describes a behavior which is not expected - a bug. and removed investigate labels Aug 11, 2023
@mkArtakMSFT mkArtakMSFT modified the milestones: 8.0-rc2, .NET 9 Planning Aug 21, 2023
@mkArtakMSFT mkArtakMSFT modified the milestones: .NET 9 Planning, 8.0.0 Oct 13, 2023
@surayya-MS
Copy link
Member

Done in this PR #51279

@ghost ghost locked as resolved and limited conversation to collaborators Nov 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

No branches or pull requests

4 participants