-
Notifications
You must be signed in to change notification settings - Fork 8
/
UrlCombine.cs
47 lines (39 loc) · 1.67 KB
/
UrlCombine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Linq;
namespace UrlCombineLib
{
public static class UrlCombine
{
/// <summary>
/// Combines the url base and the relative url into one, consolidating the '/' between them
/// </summary>
/// <param name="urlBase">Base url that will be combined</param>
/// <param name="relativeUrl">The relative path to combine</param>
/// <returns>The merged url</returns>
public static string Combine(string baseUrl, string relativeUrl)
{
if (string.IsNullOrWhiteSpace(baseUrl))
throw new ArgumentNullException(nameof(baseUrl));
if (string.IsNullOrWhiteSpace(relativeUrl))
return baseUrl;
baseUrl = baseUrl.TrimEnd('/');
relativeUrl = relativeUrl.TrimStart('/');
return $"{baseUrl}/{relativeUrl}";
}
/// <summary>
/// Combines the url base and the array of relatives urls into one, consolidating the '/' between them
/// </summary>
/// <param name="urlBase">Base url that will be combined</param>
/// <param name="relativeUrl">The array of relative paths to combine</param>
/// <returns>The merged url</returns>
public static string Combine(string baseUrl, params string[] relativePaths)
{
if (string.IsNullOrWhiteSpace(baseUrl))
throw new ArgumentNullException(nameof(baseUrl));
if (relativePaths.Length == 0)
return baseUrl;
var currentUrl = Combine(baseUrl, relativePaths[0]);
return Combine(currentUrl, relativePaths.Skip(1).ToArray());
}
}
}