dotnet/maui · error · ArgumentException
'selector' cannot be null or whitespace.
Error message
'selector' cannot be null or whitespace.
What it means
The WinForms RootComponent constructor validates the selector parameter immediately (unlike the XAML-based MAUI/WPF variants which validate at add-time). Because WinForms constructs RootComponent in code with explicit constructor arguments, the validation can happen at construction. A null or whitespace selector means there is no CSS target in the host page for the component.
Source
Thrown at src/BlazorWebView/src/WindowsForms/RootComponent.cs:26
namespace Microsoft.AspNetCore.Components.WebView.WindowsForms
{
/// <summary>
/// Describes a root component that can be added to a <see cref="BlazorWebView"/>.
/// </summary>
public class RootComponent
{
/// <summary>
/// Constructs an instance of <see cref="RootComponent"/>.
/// </summary>
/// <param name="selector">The CSS selector string that specifies where in the document the component should be placed. This must be unique among the root components within the <see cref="BlazorWebView"/>.</param>
/// <param name="componentType">The type of the root component. This type must implement <see cref="IComponent"/>.</param>
/// <param name="parameters">An optional dictionary of parameters to pass to the root component.</param>
public RootComponent(string selector, Type componentType, IDictionary<string, object?>? parameters)
{
if (string.IsNullOrWhiteSpace(selector))
{
throw new ArgumentException($"'{nameof(selector)}' cannot be null or whitespace.", nameof(selector));
}
Selector = selector;
ComponentType = componentType ?? throw new ArgumentNullException(nameof(componentType));
Parameters = parameters;
}
/// <summary>
/// Gets the CSS selector string that specifies where in the document the component should be placed.
/// This must be unique among the root components within the <see cref="BlazorWebView"/>.
/// </summary>
public string Selector { get; }
/// <summary>
/// Gets the type of the root component. This type must implement <see cref="IComponent"/>.
/// </summary>
public Type ComponentType { get; }
View on GitHub (pinned to f377ff1c5e)
Solutions
- Pass a valid non-empty CSS selector string as the first constructor argument.
- If selector comes from configuration, validate it before constructing: if (string.IsNullOrWhiteSpace(selector)) throw new InvalidOperationException("Selector must be configured.");
- Use a constant or well-known selector like "#app" or "#blazor-root".
- Audit all RootComponent constructor calls for null or empty selectors.
Example fix
// before
var rc = new RootComponent("", typeof(MyComponent), null);
// after
var rc = new RootComponent("#app", typeof(MyComponent), null); Defensive patterns
Strategy: validation
Validate before calling
// Validate selector before constructing RootComponent
if (string.IsNullOrWhiteSpace(selector))
{
throw new ArgumentException("Selector must be a non-empty CSS selector string.", nameof(selector));
}
var rc = new RootComponent(selector, componentType, parameters); Type guard
static bool IsValidSelector(string selector)
{
return !string.IsNullOrWhiteSpace(selector);
} Prevention
- Use a constant or well-known selector (e.g. "#app") in all RootComponent constructor calls.
- If selector comes from configuration, validate it at startup before constructing RootComponent.
- Add a unit test that constructs RootComponent with typical selector values.
- Audit constructor calls during code review for missing or empty selectors.
When it happens
Trigger: Constructing 'new RootComponent(null, typeof(MyComponent), null)' or 'new RootComponent(" ", typeof(MyComponent), null)'. The ArgumentException fires before the object is fully constructed, so the RootComponent is never added to the collection.
Common situations: Passing a variable that resolved to null or empty string as the selector; copy-paste from example code where selector was a placeholder; refactoring that introduced a null selector from a configuration value; unit test creating RootComponent with test data that included an empty selector.
Related errors
- There is no root component with selector '{selector}'.
- RootComponent requires a value for its Selector property, bu
- RootComponent requires a value for its ComponentType propert
- RootComponent requires a value for its Selector property, bu
- RootComponent requires a value for its ComponentType propert
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/f43fbfa6943b0b34.
Report an issue: GitHub.