How to properly combine URLs in C#? Analog of Path.Combine for web addresses
Hello everyone! I'm new to C# and writing a small application for working with API. I need to dynamically form URLs for different endpoints. For example, I have a base URL https://api.example.com/v1/ and I want to add a path like /users/123 or /products?page=2 to it.
I tried using Path.Combine, like for regular file paths, but it leads to strange results. Here's an example:
string baseUrl = "https://api.example.com/v1/";\nstring endpoint = "/users/123";\nstring result = Path.Combine(baseUrl, endpoint);\nConsole.WriteLine(result);\n// Output: "https:\\\\api.example.com\\\\v1\\\\users\\\\123"\n// Note: slashes are in the wrong direction!\n\nThis is complete nonsense! Path.Combine
No answers yet.
Log in to leave an answer