Path.Combine for URLs?

前端 未结 30 2472
不思量自难忘°
不思量自难忘° 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:27

    I just put together a small extension method:

    public static string UriCombine (this string val, string append)
            {
                if (String.IsNullOrEmpty(val)) return append;
                if (String.IsNullOrEmpty(append)) return val;
                return val.TrimEnd('/') + "/" + append.TrimStart('/');
            }
    

    It can be used like this:

    "www.example.com/".UriCombine("/images").UriCombine("first.jpeg");
    

提交回复
热议问题