{"record":{"id":"2006d0c7b281956d","repo":"dotnet/aspnetcore","slug":"cannot-have-empty-query-parameter-names","errorCode":null,"errorMessage":"Cannot have empty query parameter names.","messagePattern":"Cannot have empty query parameter names\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/NavigationManagerExtensions.cs","lineNumber":166,"sourceCode":"            _builder.Append(hash);\n        }\n    }\n\n    // A utility for feeding a collection of parameter values into a QueryStringBuilder.\n    // This is used when generating a querystring with a query parameter that has multiple values.\n    private readonly struct QueryParameterSource<TValue>\n    {\n        private readonly IEnumerator<TValue?>? _enumerator;\n        private readonly QueryParameterFormatter<TValue>? _formatter;\n\n        public string EncodedName { get; }\n\n        // Creates an empty instance to simulate a source without any elements.\n        public QueryParameterSource(string name)\n        {\n            if (string.IsNullOrEmpty(name))\n            {\n                throw new InvalidOperationException(EmptyQueryParameterNameExceptionMessage);\n            }\n\n            EncodedName = Uri.EscapeDataString(name);\n\n            _enumerator = default;\n            _formatter = default;\n        }\n\n        public QueryParameterSource(string name, IEnumerable<TValue?> values, QueryParameterFormatter<TValue> formatter)\n            : this(name)\n        {\n            _enumerator = values.GetEnumerator();\n            _formatter = formatter;\n        }\n\n        public bool TryAppendNextParameter(ref QueryStringBuilder builder)\n        {\n            if (_enumerator is null || !_enumerator.MoveNext())","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/NavigationManagerExtensions.cs#L148-L184","documentation":"The QueryParameterSource<TValue> constructor throws InvalidOperationException when the parameter name is null or empty. Query parameter names are required to build a valid querystring; an empty name would produce malformed output.","triggerScenarios":"Passing an empty string or null as the dictionary key/name to GetUriWithQueryParameters; a dictionary with a whitespace/empty key used for query construction; reflection-driven code producing empty parameter names.","commonSituations":"Building query parameters from dynamic data where a key is missing; deserializing a dictionary that includes empty keys; copy-paste leaving a placeholder empty key.","solutions":["Filter out null/empty keys before calling GetUriWithQueryParameters.","Validate parameter names are non-empty at the source (e.g. form binding).","Use strongly-typed overloads (GetUriWithQueryParameter<T>) which still require a name check."],"exampleFix":"// before\nvar qs = navManager.GetUriWithQueryParameters(new Dictionary<string,object?> { [\"\"] = 1 });\n\n// after\nvar qs = navManager.GetUriWithQueryParameters(parameters.Where(p => !string.IsNullOrEmpty(p.Key)).ToDictionary());","handlingStrategy":"validation","validationCode":"var filtered = parameters\n    .Where(p => !string.IsNullOrEmpty(p.Key))\n    .ToDictionary(p => p.Key, p => p.Value);\nvar uri = navManager.GetUriWithQueryParameters(filtered);","typeGuard":"static bool HasNoEmptyNames(IReadOnlyDictionary<string,object?> p) =>\n    p.All(kv => !string.IsNullOrEmpty(kv.Key));","tryCatchPattern":"try { uri = navManager.GetUriWithQueryParameters(parameters); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"empty query parameter names\"))\n{ logger.LogWarning(ex, \"Filtered out empty query names\");\n  uri = navManager.GetUriWithQueryParameters(parameters.Where(kv => !string.IsNullOrEmpty(kv.Key)).ToDictionary()); }","preventionTips":["Sanitize dynamic dictionaries before building query strings.","Use constants or nameof for parameter names.","Add a unit test asserting no empty keys reach the API."],"tags":["blazor","query-string","validation","navigation"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}