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");
 }
1
  • Side note: you want || , not | in boolean logic. Commented Sep 6, 2020 at 7:31

2 Answers 2

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.

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

Comments

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.

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.