{"record":{"id":"766809e3c4c6a105","repo":"microsoft/aspire","slug":"apipath-is-required-when-apitarget-is-specified","errorCode":null,"errorMessage":"apiPath is required when apiTarget is specified.","messagePattern":"apiPath is required when apiTarget is specified\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.JavaScript/JavaScriptHostingExtensions.cs","lineNumber":1296,"sourceCode":"    [Experimental(\"ASPIREJAVASCRIPT001\", UrlFormat = \"https://aka.ms/aspire/diagnostics/{0}\")]\n    private static IResourceBuilder<TResource> PublishAsStaticWebsiteCore<TResource>(\n        IResourceBuilder<TResource> builder,\n        string? apiPath,\n        IResourceBuilder<IResourceWithServiceDiscovery>? apiTarget,\n        PublishAsStaticWebsiteOptions options)\n        where TResource : JavaScriptAppResource\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrEmpty(options.OutputPath);\n\n        if (apiPath is not null && apiTarget is null)\n        {\n            throw new ArgumentException(\"apiTarget is required when apiPath is specified.\", nameof(apiTarget));\n        }\n\n        if (apiTarget is not null && apiPath is null)\n        {\n            throw new ArgumentException(\"apiPath is required when apiTarget is specified.\", nameof(apiPath));\n        }\n\n        if (apiPath is not null && apiTarget is not null)\n        {\n            if (!apiPath.StartsWith('/'))\n            {\n                throw new ArgumentException(\"The apiPath must start with '/'.\", nameof(apiPath));\n            }\n\n            apiPath = apiPath.TrimEnd('/');\n\n            if (apiPath.Length == 0)\n            {\n                throw new ArgumentException(\"The apiPath must not be '/' — it would match all requests and make the static site unreachable.\", nameof(apiPath));\n            }\n\n            ValidateApiPath(apiPath);\n            builder.WithReference(apiTarget);","sourceCodeStart":1278,"sourceCodeEnd":1314,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.JavaScript/JavaScriptHostingExtensions.cs#L1278-L1314","documentation":"Mirror case of the apiPath-without-target check: you supplied an apiTarget resource but no apiPath. Both are required because the library must know under which URL prefix to proxy requests to the target; it refuses to pick a default prefix implicitly.","triggerScenarios":"Calling the API-proxy extension with apiTarget set and apiPath null, e.g. .WithApiProxy(apiTarget: apiBuilder).","commonSituations":"Refactoring where the path was extracted into a variable that became null; copying only the target argument from docs; assuming a default \"/api\" path exists (it does not).","solutions":["Provide an explicit apiPath along with apiTarget, e.g. .WithApiProxy(apiPath: \"/api\", apiTarget: apiBuilder).","If no proxying is needed, remove the apiTarget argument.","Default the path at the call site (apiPath ??= \"/api\") only if a conventional prefix is acceptable in your app."],"exampleFix":"// before\n.WithApiProxy(apiTarget: apiBuilder);\n// after\n.WithApiProxy(apiPath: \"/api\", apiTarget: apiBuilder);","handlingStrategy":"validation","validationCode":"if (apiTarget is not null && string.IsNullOrEmpty(apiPath))\n    throw new ArgumentException(\"apiPath is required when apiTarget is specified.\");","typeGuard":null,"tryCatchPattern":"try { builder.WithApiProxy(apiPath, apiTarget); }\ncatch (ArgumentException ex) when (ex.ParamName == \"apiPath\")\n{\n    apiPath = \"/api\"; // or skip proxy configuration\n}","preventionTips":["Pass apiPath and apiTarget as a pair in a single call.","Do not assume a default proxy path exists.","Centralize proxy setup in one helper."],"tags":["aspire","javascript","argument-validation","api-proxy"],"backgroundTag":"missing-required-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}