microsoft/aspire · error · ArgumentNullException

ArgumentNullException for parameter 'pattern' (PathString…

Error message

ArgumentNullException for parameter 'pattern' (PathString value is null).

What it means

WithTransformPathRouteValues takes a route pattern as PathString and rejects a null Value with ArgumentNullException for 'pattern'. The pattern is required because it defines the route template with the {**route_values} style path placeholders YARP expands.

Solutions

  1. Pass a valid route pattern string, e.g. "/products/{**remainder}".
  2. Switch to the string-based overload (the PathString overload is ignored in polyglot app hosts).
  3. Coalesce or validate the pattern before calling: string.IsNullOrEmpty check.
  4. Ensure the constant/config source that should hold the pattern actually has a value.

Example fix

// before
route.WithTransformPathRouteValues(new PathString(pattern)); // pattern null
// after
route.WithTransformPathRouteValues(pattern ?? "/products/{**remainder}");
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(pattern.Value))
    throw new ArgumentException("pattern must be non-empty before calling WithTransformPathRouteValues");

Type guard

static bool HasValue(PathString p) => !string.IsNullOrEmpty(p.Value);

Try / catch

try { route.WithTransformPathRouteValues(pattern); }
catch (ArgumentNullException ex) when (ex.ParamName == "pattern") { /* provide fallback pattern */ }

Prevention

When it happens

Trigger: Calling WithTransformPathRouteValues with default(PathString), new PathString(null), or a null string used as the route pattern.

Common situations: Pattern stored in nullable config or database column that is empty; null returned from a pattern-resolution helper; typo'd constant left null.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/6cb55fc9a7fd5699. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Yarp/ConfigurationBuilder/Transforms/PathTransformExtensions.cs:121

    [AspireExport]
    internal static YarpRoute WithTransformPathRemovePrefix(this YarpRoute route, string prefix)
    {
        ArgumentNullException.ThrowIfNull(route);
        ArgumentNullException.ThrowIfNull(prefix);

        return route.WithTransformPathRemovePrefix(new PathString(prefix));
    }

    /// <summary>
    /// Adds the transform which will set the request path with the given value.
    /// </summary>
    /// <remarks>This overload is not available in polyglot app hosts. Use the string-based overload instead.</remarks>
    [AspireExportIgnore(Reason = "PathString is not ATS-compatible. Use the string-based overload instead.")]
    public static YarpRoute WithTransformPathRouteValues(this YarpRoute route, [StringSyntax("Route")] PathString pattern)
    {
        if (pattern.Value is null)
        {
            throw new ArgumentNullException(nameof(pattern));
        }

        route.Configure(r => r.WithTransformPathRouteValues(pattern));

        return route;
    }

    /// <summary>
    /// Adds the transform which will set the request path with route values.
    /// </summary>
    /// <param name="route">The route to configure.</param>
    /// <param name="pattern">The route pattern to apply.</param>
    /// <returns>The configured <see cref="YarpRoute"/>.</returns>
    [AspireExport]
    internal static YarpRoute WithTransformPathRouteValues(this YarpRoute route, [StringSyntax("Route")] string pattern)
    {
        ArgumentNullException.ThrowIfNull(route);
        ArgumentNullException.ThrowIfNull(pattern);

View on GitHub (pinned to 25830f84bd)