3

I am attempting to get the query string from a URL as suggested here, but I'm getting a NullReferenceException. The only difference between my code and the code in the linked post is that mine is static, and I don't see how that could cause an error.

public static class Extensions
    {
        //Other helper methods

        [Inject]
        public static NavigationManager MyNavigationManager { get; set; }

        public static string GetQueryParm(string parmName)
        {
            //Null Reference Exception is called on the line below
            var uriBuilder = new UriBuilder(MyNavigationManager.Uri);           
            var q = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
            return q[parmName] ?? "";
        }
    }

I am calling this method like:

 else if (date == null | string.IsNullOrWhiteSpace(Extensions.GetQueryParm("d")))
 {
     date = DateTime.Today.ToString("yyyy-MM-dd");
 }
Bubinga
  • 613
  • 12
  • 29

2 Answers2

6

You cannot @inject or [Inject] into a static class. The MyNavigationManager property will never be assigned to.

So forget about making this an extension method and inject it into your blazor page.

H H
  • 263,252
  • 30
  • 330
  • 514
0

Here is an extension method I use to get query parameters. I took this from Chris Sainty's blog here

public static class NavigationManagerExtensions
{
    public static bool TryGetQueryString<T>(this NavigationManager navManager, string key, out T value)
    {
        var uri = navManager.ToAbsoluteUri(navManager.Uri);

        if (QueryHelpers.ParseQuery(uri.Query).TryGetValue(key, out var valueFromQueryString))
        {
            if (typeof(T) == typeof(int) && int.TryParse(valueFromQueryString, out var valueAsInt))
            {
                value = (T)(object)valueAsInt;
                return true;
            }

            if (typeof(T) == typeof(string))
            {
                value = (T)(object)valueFromQueryString.ToString();
                return true;
            }

            if (typeof(T) == typeof(decimal) && decimal.TryParse(valueFromQueryString, out var valueAsDecimal))
            {
                value = (T)(object)valueAsDecimal;
                return true;
            }

            if (typeof(T) == typeof(Guid) && Guid.TryParse(valueFromQueryString, out var valueAsGuid))
            {
                value = (T)(object)valueAsGuid;
                return true;
            }

            if (typeof(T) == typeof(bool) && bool.TryParse(valueFromQueryString, out var valueAsBool))
            {
                value = (T)(object)valueAsBool;
                return true;
            }
        }

        value = default;
        return false;
    }
}

You can use it like this:

// for example you have this URL: /page/action?showPromo=true

_navigationManager.TryGetQueryString<bool>("showPromo", out bool _showPromo);

The _showPromo will hold the query parameter name which you can use.

Umair
  • 4,864
  • 3
  • 28
  • 47