fullstackhero/dotnet-starter-kit · error · ArgumentException

UserId must be provided.

Error message

UserId must be provided.

What it means

ToggleUserStatusCommandHandler validates that command.UserId is a non-empty string before delegating to the user service; a null/whitespace id raises ArgumentException naming the UserId property. It's a guard against activating/deactivating an unspecified user.

Solutions

  1. Always supply a valid, non-empty user id in the request body / command.
  2. Check JSON casing and property names so UserId actually binds (userId vs UserId).
  3. Add or rely on the ToggleUserStatusCommandValidator to reject empty ids earlier with a friendly message.

Example fix

// before
await mediator.Send(new ToggleUserStatusCommand { ActivateUser = true, UserId = "" });
// after
if (string.IsNullOrWhiteSpace(userId)) throw new ValidationException("UserId is required");
await mediator.Send(new ToggleUserStatusCommand { ActivateUser = true, UserId = userId });
Defensive patterns

Strategy: validation

Validate before calling

const errors = {};
if (!userId || !userId.trim()) errors.userId = 'User id is required';
if (Object.keys(errors).length) throw new ValidationError(errors);

Type guard

function hasUserId(cmd) {
  return typeof cmd?.userId === 'string' && cmd.userId.trim().length > 0;
}

Try / catch

try { await mediator.Send(new ToggleUserStatusCommand(activate, userId)); }
catch (ArgumentException ex) when (ex.ParamName == "UserId") {
    return Results.BadRequest(new { error = "UserId is required" });
}

Prevention

When it happens

Trigger: POST/PUT toggle-user-status with a body where userId is null, empty string, or whitespace — e.g. a form submitted before a user was selected, or JSON property-name mismatch so the field never binds.

Common situations: Frontend list row without a selected id; client serializing camelCase while model binding expects a different casing; calling the handler directly in tests with a default command; migration code constructing the command without UserId.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of fullstackhero/dotnet-starter-kit@3f2959e683 (2026-09-15). Data as JSON: /api/errors/fbc664bcc6bf0d5e. Report an issue: GitHub.

Appendix: source

Thrown at src/Modules/Identity/Modules.Identity/Features/v1/Users/ToggleUserStatus/ToggleUserStatusCommandHandler.cs:22

namespace FSH.Modules.Identity.Features.v1.Users.ToggleUserStatus;

public sealed class ToggleUserStatusCommandHandler : ICommandHandler<ToggleUserStatusCommand, Unit>
{
    private readonly IUserService _userService;

    public ToggleUserStatusCommandHandler(IUserService userService)
    {
        _userService = userService;
    }

    public async ValueTask<Unit> Handle(ToggleUserStatusCommand command, CancellationToken cancellationToken)
    {
        ArgumentNullException.ThrowIfNull(command);

        if (string.IsNullOrWhiteSpace(command.UserId))
        {
            throw new ArgumentException("UserId must be provided.", nameof(command.UserId));
        }

        await _userService.ToggleStatusAsync(command.ActivateUser, command.UserId, cancellationToken).ConfigureAwait(false);

        return Unit.Value;
    }
}

View on GitHub (pinned to 3f2959e683)