30

I use URL parameters for page state in my app.

How can i change the URL without actually navigating?

Thanks!

(using blazor server side)

5
  • 1
    Is using js interop ok? Commented Mar 2, 2020 at 19:52
  • 1
    Sure, but could you include how to use JavaScript interop? Commented Mar 2, 2020 at 20:15
  • 3
    Can you elaborate your Q with code sample? Commented Mar 2, 2020 at 20:52
  • 1
    give us code sample or scenario Commented Mar 3, 2020 at 6:51
  • 1
    Isn't there a way to not use the js nor NavigateTo to update the Url yet? Commented May 20, 2021 at 13:39

2 Answers 2

22

If you just want to add/remove/change query parameters in the URL, use the NavigationManager.NavigateTo method. It will not reload the page if it is not necessary (or not called with forceReload flag).

For example, the current URL is "https://example.com/page", then call NavigationManager.NavigateTo("https://example.com/page?id=1") will not reload the page but only modify URL. Click "Back" in browser will change the URL to "https://example.com/page", this change can be handled with NavigationManager.LocationChanged event.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.navigationmanager.navigateto

Sign up to request clarification or add additional context in comments.

10 Comments

The "problem" in this scenario with NavigateTo is that it would actually navigate, and initialize the page/component matching that route again, which, as I understand it, the OP are looking to avoid.
I don't think that is true. The navigation manager NavigateTo performs client-side routing and only makes server calls when it is not a client-side route. My blazor app reads in query parameters then uses NavigateTo to clear the params from the Uri. This doesn't reload my blazor app.
Technically speaking, this is the right solution. The JS interop solution seems like a hack to me. Tested in Blazor .NET7, works perfectly and does not reload the page if it is not needed. Full URL not needed, something like this also works navigationManager.NavigateTo("/counter/50");
A practical tip: Since .NET 6 you can use the extension method NavigationManager#GetUriWithQueryParameters() to create a URL based on the current URL with the required parameters set accoring to the values within the dictionary.
@Michel Jansson is right, I just had even a case like this in .net 8, it does reinitilize the component, even when you call navigateTo(..., forceLoad: false)
|
21

You can do this with JS Interop and call history.pushState(null, '', url)

Here is a simple example

.razor

@page "/"
@inject IJSRuntime jsRuntime

<input
    @bind="url"
/>

<button @onclick="ChangeUrl">
    Change Url
</button>

<p>@url</p>

@code {
    [Parameter]
    public string Url { get; set; }

    void ChangeUrl(){
        // You can also change it to any url you want
        jsRuntime.InvokeVoidAsync("ChangeUrl", Url);
    }
}

.js

window.ChangeUrl = function(url){
    history.pushState(null, '', url);   
}

Please notice that this only works for visual purpose, it will only change for the browser while in the server side, you probably won't see the change.

4 Comments

Would it get more correct if it use jsRuntime.InvokeVoidAsync("ChangeUrl", Url); on this case,as the function does not return anything?
Take in account that, after changing URL with JS, when user hit "Back" button in a browser, the browser will change URL back but application state will not change. I recommend using the NavigationManager.NavigateTo method. It will not reload the page if it is not necessary (or not called with forceReload flag). Then history will be handled properly and NavigationManager.LocationChanged event will be fired.
Don't use JavaScript use NavigiationManager instead: learn.microsoft.com/en-us/dotnet/api/…
This one-liner might be preferred: await JSRuntime.InvokeVoidAsync("history.pushState", null, "", Url);. Since history is part of window already, you don't need to write a JS function to expose it.

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.