美文网首页
UriBuilder 使用手记

UriBuilder 使用手记

作者: amosbake | 来源:发表于2018-01-29 22:59 被阅读84次

    C#中的UriBuilder 可以很方便的帮助我们拼接url,测试用例如下

    using NUnit.Framework;
    using System;
    public class UriTest
    { 
    
        [Test]
        public void UriSimplePasses()
        {
            UriBuilder uriBuilder = new UriBuilder();
            uriBuilder.Scheme = "https";
            uriBuilder.Host = "github.com";
            uriBuilder.Path = "amosbake";
            Uri uri = uriBuilder.Uri;
            Assert.AreEqual("https://github.com/amosbake", uri.ToString());
        }
    
        [Test]
        public void UriQueryPasses()
        {
            UriBuilder uriBuilder = new UriBuilder();
            uriBuilder.Scheme = "https";
            uriBuilder.Host = "api.github.com";
            uriBuilder.Path = "repos/vmg/redcarpet/issues";
            uriBuilder.Query = "state=closed";
            Uri uri = uriBuilder.Uri;
            Assert.AreEqual("https://api.github.com/repos/vmg/redcarpet/issues?state=closed", uri.ToString());
        }
    
        [Test]
        public void UriFilePasses()
        {
            UriBuilder uriBuilder = new UriBuilder();
            uriBuilder.Scheme = "c";
            uriBuilder.Host = @"temp";
            uriBuilder.Path = "log.txt";
            Uri uri = uriBuilder.Uri;
            Assert.AreEqual("file:///c:/temp/log.txt", uri.ToString());
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:UriBuilder 使用手记

          本文链接:https://www.haomeiwen.com/subject/hvntoxtx.html