Пример #1
0
        public void AppendPath(string baseUrl, string path, string expectedUrl)
        {
            // arrange

            // act
            var url = Url.AppendPath(baseUrl, path);

            // assert
            Assert.Equal(expectedUrl, url);
        }
Пример #2
0
        public void AppendPath_Format(string baseUrl, string pathFormat, string[] formatArgs, string expectedUrl)
        {
            // arrange

            // act
            var url = Url.AppendPath(baseUrl, pathFormat, formatArgs);

            // assert
            Assert.Equal(expectedUrl, url);
        }
Пример #3
0
        public static Uri WithPath(this Uri uri, string path)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            if (uri.IsAbsoluteUri)
            {
                var builder = new UriBuilder(uri);

                if (!string.IsNullOrEmpty(path))
                {
                    if (path.StartsWith("/"))
                    {
                        builder.Path = path;
                    }
                    else
                    {
                        builder.Path = builder.Path.TrimEnd('/') + "/" + path?.TrimStart('/');
                    }
                }

                return(builder.Uri);
            }
            else
            {
                string resultUrl;
                if (path.StartsWith("/"))
                {
                    resultUrl = Url.SetPath(uri.OriginalString, path);
                }
                else
                {
                    resultUrl = Url.AppendPath(uri.OriginalString, path);
                }

                return(new Uri(resultUrl, UriKind.Relative));
            }
        }