Path.Combine for URLs?

前端 未结 30 2442
不思量自难忘°
不思量自难忘° 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条回答
  •  Happy的楠姐
    2020-11-22 15:16

    There is a Todd Menier's comment above that Flurl includes a Url.Combine.

    More details:

    Url.Combine is basically a Path.Combine for URLs, ensuring one and only one separator character between parts:

    var url = Url.Combine(
        "http://MyUrl.com/",
        "/too/", "/many/", "/slashes/",
        "too", "few?",
        "x=1", "y=2"
    // result: "http://www.MyUrl.com/too/many/slashes/too/few?x=1&y=2" 
    

    Get Flurl.Http on NuGet:

    PM> Install-Package Flurl.Http

    Or get the stand-alone URL builder without the HTTP features:

    PM> Install-Package Flurl

提交回复
热议问题