Search code examples
asp.net-coreurlquery-stringblazor

How to set new value to existing query string Blazor


I'm attempting to change a query string paramater without reloading the page. I have a DateTime Date value that I'm attempting to turn into a string and put into the url. Say, for the sake of this question that it is:

DateTime? Date = new DateTime(2020, 9, 4);

So, my url could begin looking like:

https://localhost:44346/Events?d=2020-10-18

and after I do whatever magic must be done, it ends up like:

https://localhost:44346/Events?d=2020-10-04

I have attempted using NavigationManager and QueryHelpers like the following, but I've had no luck:

QueryHelpers.AddQueryString(navManager.Uri, "d", Date?.ToString("yyyy-MM-dd"));

Solution

  • Please check out this demo

        @page "/counter"
        @using Microsoft.AspNetCore.WebUtilities
        @using System.Web 
        @inject NavigationManager navManager
        
        <button class="btn btn-primary" @onclick="ChangeUrl">Change url</button>
        <br />
        demonstrate lack of page reload
        <br />
        <button class="btn btn-primary" @onclick="AddItem">Add item</button>
        @foreach (var item in list)
        {
            @item
        }
        
        @code {
        
            private List<string> list = new List<string>()
            {
                "Test string"
            };
        
            private void AddItem()
            {
                var uri = navManager.ToAbsoluteUri(navManager.Uri);
        
                if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("d", out var param))
                {
                    list.Add(param.First());
                }
                else
                {
                    list.Add("d is empty");
                }
            }
        
            DateTime dateCounter = DateTime.Today;
            private void ChangeUrl()
            {
                dateCounter = dateCounter.AddDays(1);
        
                string url = RemoveQueryStringByKey(navManager.Uri, "d");
                var query = new Dictionary<string, string> { { "d", dateCounter.ToString("dd-MM-yyyy") } };
        
                navManager.NavigateTo(QueryHelpers.AddQueryString(url, query));
            }
        
            public static string RemoveQueryStringByKey(string url, string key)
            {
                var uri = new Uri(url);
        
                // this gets all the query string key value pairs as a collection
                var newQueryString = HttpUtility.ParseQueryString(uri.Query);
        
                // this removes the key if exists
                newQueryString.Remove(key);
        
                // this gets the page path from root without QueryString
                string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path);
        
                return newQueryString.Count > 0
                    ? String.Format("{0}?{1}", pagePathWithoutQueryString, newQueryString)
                    : pagePathWithoutQueryString;
            }
        }