Path.Combine for URLs?

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

    Ryan Cook's answer is close to what I'm after and may be more appropriate for other developers. However, it adds http:// to the beginning of the string and in general it does a bit more formatting than I'm after.

    Also, for my use cases, resolving relative paths is not important.

    mdsharp's answer also contains the seed of a good idea, although that actual implementation needed a few more details to be complete. This is an attempt to flesh it out (and I'm using this in production):

    C#

    public string UrlCombine(string url1, string url2)
    {
        if (url1.Length == 0) {
            return url2;
        }
    
        if (url2.Length == 0) {
            return url1;
        }
    
        url1 = url1.TrimEnd('/', '\\');
        url2 = url2.TrimStart('/', '\\');
    
        return string.Format("{0}/{1}", url1, url2);
    }
    

    VB.NET

    Public Function UrlCombine(ByVal url1 As String, ByVal url2 As String) As String
        If url1.Length = 0 Then
            Return url2
        End If
    
        If url2.Length = 0 Then
            Return url1
        End If
    
        url1 = url1.TrimEnd("/"c, "\"c)
        url2 = url2.TrimStart("/"c, "\"c)
    
        Return String.Format("{0}/{1}", url1, url2)
    End Function
    

    This code passes the following test, which happens to be in VB:

     Public Sub UrlCombineTest()
        Dim target As StringHelpers = New StringHelpers()
    
        Assert.IsTrue(target.UrlCombine("test1", "test2") = "test1/test2")
        Assert.IsTrue(target.UrlCombine("test1/", "test2") = "test1/test2")
        Assert.IsTrue(target.UrlCombine("test1", "/test2") = "test1/test2")
        Assert.IsTrue(target.UrlCombine("test1/", "/test2") = "test1/test2")
        Assert.IsTrue(target.UrlCombine("/test1/", "/test2/") = "/test1/test2/")
        Assert.IsTrue(target.UrlCombine("", "/test2/") = "/test2/")
        Assert.IsTrue(target.UrlCombine("/test1/", "") = "/test1/")
    End Sub
    

提交回复
热议问题