Path.Combine for URLs?

前端 未结 30 2422
不思量自难忘°
不思量自难忘° 2020-11-22 14:28

Path.Combine is handy, but is there a similar function in the .NET framework for URLs?

I\'m looking for syntax like this:

Url.Combine(\"http://MyUrl.         


        
30条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 15:14

    Use:

        private Uri UriCombine(string path1, string path2, string path3 = "", string path4 = "")
        {
            string path = System.IO.Path.Combine(path1, path2.TrimStart('\\', '/'), path3.TrimStart('\\', '/'), path4.TrimStart('\\', '/'));
            string url = path.Replace('\\','/');
            return new Uri(url);
        }
    

    It has the benefit of behaving exactly like Path.Combine.

提交回复
热议问题