URL management
Path.Combine
Web Development
.NET Framework
Coding Techniques

Path.Combine for URLs in C#: what to use instead

Codemia Knowledge Hub ·

Do not use Path.Combine for URLs. It is built for file system paths, so on Windows it joins with a backslash and knows nothing about URL parts like ://, query strings or encoding. Use the Uri class (or UriBuilder), or concatenate the strings yourself while controlling the slash.

What Path.Combine does

Path.Combine takes two or more strings and joins them into one path, inserting directory separators for you, so you avoid error-prone manual concatenation:

csharp
1string path1 = "C:\\MyFolder";
2string path2 = "MyFile.txt";
3string result = Path.Combine(path1, path2);
4// result is "C:\\MyFolder\\MyFile.txt"

Why it breaks URLs

  • Separators: File paths use backslashes \\ on Windows and forward slashes / on UNIX-based systems. URLs always use /, whatever the platform.
  • URL components: URLs contain protocol delimiters (://), query strings (?) and fragments (#). File paths have no equivalent, and Path.Combine can misinterpret them.
  • Encoding: URLs often need special characters encoded (a space as %20, for example). Path.Combine does not do this.
csharp
1string baseUrl = "http://example.com/folder";
2string file = "file.txt";
3string result = Path.Combine(baseUrl, file);
4// result is "http://example.com/folder\\file.txt" which is invalid

Combining URLs with the Uri class

csharp
var baseUri = new Uri("http://example.com/folder");
var fullUri = new Uri(baseUri, "file.txt");
// fullUri.ToString() outputs "http://example.com/file.txt"

Be careful with the trailing slash. new Uri(baseUri, relative) resolves the relative part the way a browser resolves a link, so if the base does not end in /, its last segment is replaced, which is why folder is dropped above. Write the base as http://example.com/folder/ to get http://example.com/folder/file.txt.

Concatenating manually

Trim the trailing slash from the base and add exactly one:

csharp
1string baseUrl = "http://example.com/folder";
2string file = "file.txt";
3string result = $"{baseUrl.TrimEnd('/')}/{file}";
4// result is "http://example.com/folder/file.txt"

Path.Combine vs. Uri

FeaturePath.CombineUri Class/Manual Handling
Ease of UseHigh for file pathsModerate for URLs
ReliabilityHigh for file pathsHigh for URLs
Correct Separator HandlingOS-dependentAlways correct for URLs
Support for URL ComponentsNoneFull support
Error-ProneLow for file paths, High for URLsLow

Building URLs safely

Careful encoding and string handling when building URLs also helps prevent security problems such as open redirects and injection attacks.

  • Validate URLs, especially when they include user input.
  • Use UriBuilder when you need to change individual parts of a URL.
  • Test URL construction in the different environments you deploy to.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms