dotnet/yarp · error · ArgumentException

'{nameof(toMethod)}' cannot be null or empty.

Error message

'{nameof(toMethod)}' cannot be null or empty.

What it means

Thrown by the HttpMethodChangeTransform constructor when `toMethod` is null or empty. The target method is what requests are rewritten to, so a blank value is rejected as ArgumentException. Distinct from the `fromMethod` check so the caller knows which argument is bad.

Source

Thrown at src/ReverseProxy/Transforms/HttpMethodChangeTransform.cs:30

/// Replaces the HTTP method if it matches.
/// </summary>
public class HttpMethodChangeTransform : RequestTransform
{
    /// <summary>
    /// Creates a new transform.
    /// </summary>
    /// <param name="fromMethod">The method to match.</param>
    /// <param name="toMethod">The method to it change to.</param>
    public HttpMethodChangeTransform(string fromMethod, string toMethod)
    {
        if (string.IsNullOrEmpty(fromMethod))
        {
            throw new ArgumentException($"'{nameof(fromMethod)}' cannot be null or empty.", nameof(fromMethod));
        }

        if (string.IsNullOrEmpty(toMethod))
        {
            throw new ArgumentException($"'{nameof(toMethod)}' cannot be null or empty.", nameof(toMethod));
        }

        FromMethod = GetCanonicalizedValue(fromMethod);
        ToMethod = GetCanonicalizedValue(toMethod);
    }

    internal HttpMethod FromMethod { get; }

    internal HttpMethod ToMethod { get; }

    /// <inheritdoc/>
    public override ValueTask ApplyAsync(RequestTransformContext context)
    {
        if (FromMethod.Equals(context.ProxyRequest.Method))
        {
            context.ProxyRequest.Method = ToMethod;
        }

View on GitHub (pinned to bd11867bee)

Solutions

  1. Supply a valid non-empty HTTP method for `toMethod`.
  2. Guard the value before constructing if sourced from input.
  3. Complete the transform config with both from- and to-methods.

Example fix

// before
new HttpMethodChangeTransform("POST", "");
// after
new HttpMethodChangeTransform("POST", "PUT");
Defensive patterns

Strategy: validation

Validate before calling

ArgumentException.ThrowIfNullOrEmpty(toMethod);

Type guard

static bool IsValidHttpMethod(string? m) =>
    !string.IsNullOrEmpty(m) && m.All(c => c >= 'A' && c <= 'Z' || c == '-');

Prevention

When it happens

Trigger: Constructing `new HttpMethodChangeTransform(fromMethod, toMethod)` with `toMethod` null/empty/whitespace, or transform config that omits/blanks the to-method.

Common situations: Config has `from` but missing `to`. Programmatic construction from a config value that defaulted to empty.

Related errors


AI-assisted analysis of dotnet/yarp@bd11867bee (2026-08-13). Data as JSON: /api/errors/fa229729db431fa6. Report an issue: GitHub.