Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Manager NullReferenceException

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");
 }
like image 574
Bubinga Avatar asked Nov 17 '25 23:11

Bubinga


1 Answers

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.

like image 158
Henk Holterman Avatar answered Nov 20 '25 13:11

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!