OrchardCMS/OrchardCore · error · ArgumentException

The 'key' cannot be null or empty

Error message

The 'key' cannot be null or empty

What it means

PermissionGroupKey's constructor validates that the group key is not null/empty/whitespace and throws ArgumentException with message "The 'key' cannot be null or empty". It is a fail-fast guard ensuring role permission groups always have an identifier.

Solutions

  1. Ensure the string passed as key is non-null and non-whitespace before constructing
  2. Validate source data (recipe/JSON/provider output) for the key field
  3. Default the key to a stable identifier when the source omits it

Example fix

// before
var group = new PermissionGroupKey(source.Key);
// after
var group = string.IsNullOrWhiteSpace(source.Key) ? null : new PermissionGroupKey(source.Key);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("key required", nameof(key)); var g = new PermissionGroupKey(key);

Type guard

bool IsValidKey(string k) => !string.IsNullOrWhiteSpace(k);

Try / catch

try { group = new PermissionGroupKey(key); } catch (ArgumentException) { log.LogWarning("Skipped permission group with empty key"); }

Prevention

When it happens

Trigger: Calling new PermissionGroupKey(null), PermissionGroupKey(""), or PermissionGroupKey(" "), including via the (key, title) overload which chains to this constructor; e.g. building permission groups from data where the key field is missing.

Common situations: Custom role permission providers returning groups with missing keys, deserializing permission group definitions from JSON/config with absent key values.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/9467e186a3ea919b. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore.Modules/OrchardCore.Roles/ViewModels/PermissionGroupKey.cs:15

namespace OrchardCore.Roles.ViewModels;

public class PermissionGroupKey
{
    public readonly string Key;

    public readonly string Title;

    public string Source { get; set; }

    public PermissionGroupKey(string key)
    {
        if (string.IsNullOrWhiteSpace(key))
        {
            throw new ArgumentException($"The '{nameof(key)}' cannot be null or empty", nameof(key));
        }

        Key = key;
    }

    public PermissionGroupKey(string key, string title) : this(key)
    {
        Title = title;
    }

    public override int GetHashCode() => Key.GetHashCode();

    public override bool Equals(object obj)
    {
        var other = obj as PermissionGroupKey;

        return other != null && Key.Equals(other.Key, StringComparison.Ordinal);
    }

View on GitHub (pinned to 4306c0717f)