/// <summary>
        /// 创建新的uri
        /// </summary>
        /// <param name="uri">原始uri</param>
        /// <param name="keyValues">键值对</param>
        /// <returns></returns>
        protected virtual Uri CreateUri(Uri uri, IEnumerable <KeyValue> keyValues)
        {
            var uriValue = new UriValue(uri);

            foreach (var keyValue in keyValues)
            {
                uriValue = uriValue.Replace(keyValue.Key, keyValue.Value, out var replaced);
                if (replaced == false)
                {
                    uriValue = uriValue.AddQuery(keyValue.Key, keyValue.Value);
                }
            }
            return(uriValue.ToUri());
        }
Exemplo n.º 2
0
        public void ReplaceTest()
        {
            var uriValue = new UriValue("http://www.webapiclient.com");

            uriValue.Replace("a", "a", out var replaced);
            Assert.False(replaced);

            uriValue = new UriValue("http://www.webapiclient.com/path/?x={x}&y={Y}");
            uriValue = uriValue.Replace("x", "x", out replaced);
            Assert.True(replaced);
            Assert.Equal("http://www.webapiclient.com/path/?x=x&y={Y}", uriValue.ToString());

            uriValue = new UriValue("http://www.webapiclient.com/path/?x={x}&y={Y}");
            uriValue = uriValue.Replace("y", "y", out replaced);
            Assert.True(replaced);
            Assert.Equal("http://www.webapiclient.com/path/?x={x}&y=y", uriValue.ToString());
        }