dotnet/aspnetcore · error · InvalidOperationException
Cannot have empty query parameter names.
Error message
Cannot have empty query parameter names.
What it means
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.
Source
Thrown at src/Components/Components/src/NavigationManagerExtensions.cs:166
_builder.Append(hash);
}
}
// A utility for feeding a collection of parameter values into a QueryStringBuilder.
// This is used when generating a querystring with a query parameter that has multiple values.
private readonly struct QueryParameterSource<TValue>
{
private readonly IEnumerator<TValue?>? _enumerator;
private readonly QueryParameterFormatter<TValue>? _formatter;
public string EncodedName { get; }
// Creates an empty instance to simulate a source without any elements.
public QueryParameterSource(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new InvalidOperationException(EmptyQueryParameterNameExceptionMessage);
}
EncodedName = Uri.EscapeDataString(name);
_enumerator = default;
_formatter = default;
}
public QueryParameterSource(string name, IEnumerable<TValue?> values, QueryParameterFormatter<TValue> formatter)
: this(name)
{
_enumerator = values.GetEnumerator();
_formatter = formatter;
}
public bool TryAppendNextParameter(ref QueryStringBuilder builder)
{
if (_enumerator is null || !_enumerator.MoveNext())View on GitHub (pinned to 294cab2f9b)
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.
Example fix
// before
var qs = navManager.GetUriWithQueryParameters(new Dictionary<string,object?> { [""] = 1 });
// after
var qs = navManager.GetUriWithQueryParameters(parameters.Where(p => !string.IsNullOrEmpty(p.Key)).ToDictionary()); Defensive patterns
Strategy: validation
Validate before calling
var filtered = parameters
.Where(p => !string.IsNullOrEmpty(p.Key))
.ToDictionary(p => p.Key, p => p.Value);
var uri = navManager.GetUriWithQueryParameters(filtered); Type guard
static bool HasNoEmptyNames(IReadOnlyDictionary<string,object?> p) =>
p.All(kv => !string.IsNullOrEmpty(kv.Key)); Try / catch
try { uri = navManager.GetUriWithQueryParameters(parameters); }
catch (InvalidOperationException ex) when (ex.Message.Contains("empty query parameter names"))
{ logger.LogWarning(ex, "Filtered out empty query names");
uri = navManager.GetUriWithQueryParameters(parameters.Where(kv => !string.IsNullOrEmpty(kv.Key)).ToDictionary()); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The URI '{uri}' is not contained by the base URI '{baseUri}'
- Cannot format query parameters with values of type '{underly
- The options parameter is required.
- The custom event '${eventName}' cannot have the same name as
- initialParameters must be an object, even if empty.
AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06).
Data as JSON: /api/errors/2006d0c7b281956d.
Report an issue: GitHub.