Tanoda
UrlCombine.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3public static class UrlCombine
4{
11 public static string Combine(string baseUrl, string relativeUrl)
12 {
13 if (string.IsNullOrWhiteSpace(baseUrl))
14 throw new ArgumentNullException(nameof(baseUrl));
15
16 if (string.IsNullOrWhiteSpace(relativeUrl))
17 return baseUrl;
18
19 baseUrl = baseUrl.TrimEnd('/');
20 relativeUrl = relativeUrl.TrimStart('/');
21
22 return $"{baseUrl}/{relativeUrl}";
23 }
24
31 public static string Combine(string baseUrl, params string[] relativePaths)
32 {
33 if (string.IsNullOrWhiteSpace(baseUrl))
34 throw new ArgumentNullException(nameof(baseUrl));
35
36 if (relativePaths.Length == 0)
37 return baseUrl;
38
39 var currentUrl = Combine(baseUrl, relativePaths[0]);
40
41 return Combine(currentUrl, relativePaths.Skip(1).ToArray());
42 }
43}