| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="UriBuilder"/>. |
| | 5 | | /// </summary> |
| | 6 | | public static class UriBuilderExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Returns <see cref="UriBuilder"/> |
| | 10 | | /// with the specified path. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="builder">The builder.</param> |
| | 13 | | /// <param name="path">The path.</param> |
| | 14 | | public static UriBuilder? WithPath(this UriBuilder? builder, string? path) |
| 0 | 15 | | { |
| 0 | 16 | | if (builder == null) return null; |
| 0 | 17 | | if (string.IsNullOrWhiteSpace(path)) return builder; |
| | 18 | |
|
| | 19 | | const string delimiter = "/"; |
| | 20 | | const char delimiterChar = '/'; |
| | 21 | |
|
| 0 | 22 | | var baseSegments = builder.Uri.Segments |
| 0 | 23 | | .Where(i => i != delimiter) |
| 0 | 24 | | .Select(i => i.Trim(delimiterChar)); |
| 0 | 25 | | var pathSegments = path.Split(delimiterChar) |
| 0 | 26 | | .Where(i => !string.IsNullOrWhiteSpace(i)); |
| 0 | 27 | | var combinedSegments = baseSegments.Union(pathSegments); |
| | 28 | |
|
| 0 | 29 | | builder.Path = string.Join(delimiter, combinedSegments.ToArray()); |
| | 30 | |
|
| 0 | 31 | | return builder; |
| 0 | 32 | | } |
| | 33 | | } |