{"record":{"id":"bfe09e691297e274","repo":"reactiveui/refit","slug":"hosturl-must-not-be-null-or-whitespace","errorCode":null,"errorMessage":"`hostUrl` must not be null or whitespace.","messagePattern":"`hostUrl` must not be null or whitespace\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Refit/RestService.cs","lineNumber":398,"sourceCode":"            DynamicallyAccessedMemberTypes.Interfaces\n            | DynamicallyAccessedMemberTypes.PublicMethods\n            | DynamicallyAccessedMemberTypes.NonPublicMethods)]\n        Type refitInterfaceType,\n        string hostUrl) => For(refitInterfaceType, hostUrl, null);\n\n    /// <summary>Create an <see cref=\"HttpClient\"/> with <paramref name=\"hostUrl\"/> as the base address.</summary>\n    /// <param name=\"hostUrl\">Base address.</param>\n    /// <param name=\"settings\"><see cref=\"RefitSettings\"/> to use to configure the HttpClient.</param>\n    /// <returns>A <see cref=\"HttpClient\"/> with the various parameters provided.</returns>\n    /// <exception cref=\"ArgumentException\">Thrown when <paramref name=\"hostUrl\"/> is null or whitespace.</exception>\n    public static HttpClient CreateHttpClient(string hostUrl, RefitSettings? settings)\n    {\n#if NET8_0_OR_GREATER\n        ArgumentException.ThrowIfNullOrWhiteSpace(hostUrl);\n#else\n        if (string.IsNullOrWhiteSpace(hostUrl))\n        {\n            throw new ArgumentException(\n                $\"`{nameof(hostUrl)}` must not be null or whitespace.\",\n                nameof(hostUrl));\n        }\n#endif\n\n        // check to see if user provided custom auth token\n        HttpMessageHandler? innerHandler = null;\n        if (settings is not null)\n        {\n            if (settings.HttpMessageHandlerFactory is not null)\n            {\n                innerHandler = settings.HttpMessageHandlerFactory();\n            }\n\n            if (settings.AuthorizationHeaderValueGetter is not null)\n            {\n                innerHandler = new AuthenticatedHttpClientHandler(\n                    settings.AuthorizationHeaderValueGetter,","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/RestService.cs#L380-L416","documentation":"RestService.CreateHttpClient throws ArgumentException when hostUrl is null, empty, or whitespace, because it becomes the HttpClient.BaseAddress and an empty base address is unusable. On .NET 8+ it delegates to ArgumentException.ThrowIfNullOrWhiteSpace; otherwise it checks manually and throws with the parameter name.","triggerScenarios":"Calling RestService.CreateHttpClient(null, settings), RestService.For<T>(\"\"), or passing a hostUrl loaded from a missing config key that resolves to null or empty.","commonSituations":"Reading the API base URL from IConfiguration/IOptions and the section being absent; environment variable not set in production; typo in the config key; default(string) passed through a helper.","solutions":["Pass a non-empty absolute URL string, e.g. \"https://api.example.com\"","Validate the host at startup and fail fast with a clear message if config is missing","Provide a fallback/default URL via the null-coalescing operator when reading config","Bind the URL through IOptions<ApiOptions> with data annotations validation"],"exampleFix":"// before\nvar host = config[\"Api:BaseUrl\"]; // null if missing\nvar client = RestService.CreateHttpClient(host, settings); // throws\n\n// after\nvar host = config[\"Api:BaseUrl\"]\n    ?? throw new InvalidOperationException(\"Api:BaseUrl is not configured\");\nvar client = RestService.CreateHttpClient(host, settings);","handlingStrategy":"validation","validationCode":"var host = configuration[\"Api:BaseUrl\"];\nif (string.IsNullOrWhiteSpace(host))\n    throw new InvalidOperationException(\"Api:BaseUrl is not configured.\");\nvar client = RestService.CreateHttpClient(host, settings);","typeGuard":"static bool IsValidHost(string? host) =>\n    !string.IsNullOrWhiteSpace(host);","tryCatchPattern":"try { var client = RestService.CreateHttpClient(host, settings); }\ncatch (ArgumentException ex) when (ex.ParamName == nameof(host))\n{\n    // surface a clear configuration error to the operator\n}","preventionTips":["Validate the host URL at startup and fail fast with a clear message","Bind the URL through validated IOptions<T> with [Required] data annotations","Provide a non-empty default for local development via user secrets","Add a config presence check in a health endpoint"],"tags":["configuration","http-client","argument","base-address","argumentexception"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}