Avatar ·

How to correctly combine URL parts in C#? Analog of Path.Combine for URLs

Hi everyone! I'm new to C# and ran into this problem. I need to programmatically form URLs by combining a base URL with relative paths. .NET has a great `Path.Combine` method that works perfectly with file paths, but when I try to use it for URLs, I get garbage. Here's an example of my code: ```csharp string baseUrl = "https://example.com/api/"; string endpoint = "users/get"; string fullUrl = Path.Combine(baseUrl, endpoint); Console.WriteLine(fullUrl); ``` Instead of the expected `https://example.com/api/users/get`, I get `https://example.com/api\users\get`. Backslashes instead of forward slashes! And if I remove the trailing slash in `baseUrl`, the result is `https://example.com/api\users\get` — meaning part of the path is simply lost or joined incorrectly.