Path.Combine for URLs?

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

    I find the following useful and has the following features :

    • Throws on null or white space
    • Takes multiple params parameter for multiple Url segments
    • throws on null or empty

    Class

    public static class UrlPath
    {
       private static string InternalCombine(string source, string dest)
       {
          if (string.IsNullOrWhiteSpace(source))
             throw new ArgumentException("Cannot be null or white space", nameof(source));
    
          if (string.IsNullOrWhiteSpace(dest))
             throw new ArgumentException("Cannot be null or white space", nameof(dest));
    
          return $"{source.TrimEnd('/', '\\')}/{dest.TrimStart('/', '\\')}";
       }
    
       public static string Combine(string source, params string[] args) 
           => args.Aggregate(source, InternalCombine);
    }
    

    Tests

    UrlPath.Combine("test1", "test2");
    UrlPath.Combine("test1//", "test2");
    UrlPath.Combine("test1", "/test2");
    
    // Result = test1/test2
    
    UrlPath.Combine(@"test1\/\/\/", @"\/\/\\\\\//test2", @"\/\/\\\\\//test3\") ;
    
    // Result = test1/test2/test3
    
    UrlPath.Combine("/test1/", "/test2/", null);
    UrlPath.Combine("", "/test2/");
    UrlPath.Combine("/test1/", null);
    
    // Throws an ArgumentException
    
    0 讨论(0)
  • 2020-11-22 15:19

    For anyone who is looking for a one-liner and simply wants to join parts of a path without creating a new method or referencing a new library or construct a URI value and convert that to a string, then...

    string urlToImage = String.Join("/", "websiteUrl", "folder1", "folder2", "folder3", "item");
    

    It's pretty basic, but I don't see what more you need. If you're afraid of doubled '/' then you can simply do a .Replace("//", "/") afterward. If you're afraid of replacing the doubled '//' in 'https://', then instead do one join, replace the doubled '/', then join the website url (however I'm pretty sure most browsers will automatically convert anything with 'https:' in the front of it to read in the correct format). This would look like:

    string urlToImage = String.Join("/","websiteUrl", String.Join("/", "folder1", "folder2", "folder3", "item").Replace("//","/"));
    

    There are plenty of answers here that will handle all the above, but in my case, I only needed it once in one location and won't need to heavily rely on it. Also, it's really easy to see what is going on here.

    See: https://docs.microsoft.com/en-us/dotnet/api/system.string.join?view=netframework-4.8

    0 讨论(0)
  • 2020-11-22 15:21

    So I have another approach, similar to everyone who used UriBuilder.

    I did not want to split my BaseUrl (which can contain a part of the path - e.g. http://mybaseurl.com/dev/) as javajavajavajavajava did.

    The following snippet shows the code + Tests.

    Beware: This solution lowercases the host and appends a port. If this is not desired, one can write a string representation by e.g. leveraging the Uri Property of UriBuilder.

      public class Tests
      {
             public static string CombineUrl (string baseUrl, string path)
             {
               var uriBuilder = new UriBuilder (baseUrl);
               uriBuilder.Path = Path.Combine (uriBuilder.Path, path);
               return uriBuilder.ToString();
             }
    
             [TestCase("http://MyUrl.com/", "/Images/Image.jpg", "http://myurl.com:80/Images/Image.jpg")]
             [TestCase("http://MyUrl.com/basePath", "/Images/Image.jpg", "http://myurl.com:80/Images/Image.jpg")]
             [TestCase("http://MyUrl.com/basePath", "Images/Image.jpg", "http://myurl.com:80/basePath/Images/Image.jpg")]
             [TestCase("http://MyUrl.com/basePath/", "Images/Image.jpg", "http://myurl.com:80/basePath/Images/Image.jpg")]
             public void Test1 (string baseUrl, string path, string expected)
             {
               var result = CombineUrl (baseUrl, path);
    
               Assert.That (result, Is.EqualTo (expected));
             }
      }
    

    Tested with .NET Core 2.1 on Windows 10.

    Why does this work?

    Even though Path.Combine will return Backslashes (on Windows atleast), the UriBuilder handles this case in the Setter of Path.

    Taken from https://github.com/dotnet/corefx/blob/master/src/System.Private.Uri/src/System/UriBuilder.cs (mind the call to string.Replace)

    [AllowNull]
    public string Path
    {
          get
          {
              return _path;
          }
          set
          {
              if ((value == null) || (value.Length == 0))
              {
                  value = "/";
              }
              _path = Uri.InternalEscapeString(value.Replace('\\', '/'));
              _changed = true;
          }
     }
    

    Is this the best approach?

    Certainly this solution is pretty self describing (at least in my opinion). But you are relying on undocumented (at least I found nothing with a quick google search) "feature" from the .NET API. This may change with a future release so please cover the Method with Tests.

    There are tests in https://github.com/dotnet/corefx/blob/master/src/System.Private.Uri/tests/FunctionalTests/UriBuilderTests.cs (Path_Get_Set) which check, if the \ is correctly transformed.

    Side Note: One could also work with the UriBuilder.Uri property directly, if the uri will be used for a System.Uri ctor.

    0 讨论(0)
  • 2020-11-22 15:23

    I created this function that will make your life easier:

        /// <summary>
        /// The ultimate Path combiner of all time
        /// </summary>
        /// <param name="IsURL">
        /// true - if the paths are Internet URLs, false - if the paths are local URLs, this is very important as this will be used to decide which separator will be used.
        /// </param>
        /// <param name="IsRelative">Just adds the separator at the beginning</param>
        /// <param name="IsFixInternal">Fix the paths from within (by removing duplicate separators and correcting the separators)</param>
        /// <param name="parts">The paths to combine</param>
        /// <returns>the combined path</returns>
        public static string PathCombine(bool IsURL , bool IsRelative , bool IsFixInternal , params string[] parts)
        {
            if (parts == null || parts.Length == 0) return string.Empty;
            char separator = IsURL ? '/' : '\\';
    
            if (parts.Length == 1 && IsFixInternal)
            {
                string validsingle;
                if (IsURL)
                {
                    validsingle = parts[0].Replace('\\' , '/');
                }
                else
                {
                    validsingle = parts[0].Replace('/' , '\\');
                }
                validsingle = validsingle.Trim(separator);
                return (IsRelative ? separator.ToString() : string.Empty) + validsingle;
            }
    
            string final = parts
                .Aggregate
                (
                (string first , string second) =>
                {
                    string validfirst;
                    string validsecond;
                    if (IsURL)
                    {
                        validfirst = first.Replace('\\' , '/');
                        validsecond = second.Replace('\\' , '/');
                    }
                    else
                    {
                        validfirst = first.Replace('/' , '\\');
                        validsecond = second.Replace('/' , '\\');
                    }
                    var prefix = string.Empty;
                    if (IsFixInternal)
                    {
                        if (IsURL)
                        {
                            if (validfirst.Contains("://"))
                            {
                                var tofix = validfirst.Substring(validfirst.IndexOf("://") + 3);
                                prefix = validfirst.Replace(tofix , string.Empty).TrimStart(separator);
    
                                var tofixlist = tofix.Split(new[] { separator } , StringSplitOptions.RemoveEmptyEntries);
    
                                validfirst = separator + string.Join(separator.ToString() , tofixlist);
                            }
                            else
                            {
                                var firstlist = validfirst.Split(new[] { separator } , StringSplitOptions.RemoveEmptyEntries);
                                validfirst = string.Join(separator.ToString() , firstlist);
                            }
    
                            var secondlist = validsecond.Split(new[] { separator } , StringSplitOptions.RemoveEmptyEntries);
                            validsecond = string.Join(separator.ToString() , secondlist);
                        }
                        else
                        {
                            var firstlist = validfirst.Split(new[] { separator } , StringSplitOptions.RemoveEmptyEntries);
                            var secondlist = validsecond.Split(new[] { separator } , StringSplitOptions.RemoveEmptyEntries);
    
                            validfirst = string.Join(separator.ToString() , firstlist);
                            validsecond = string.Join(separator.ToString() , secondlist);
                        }
                    }
                    return prefix + validfirst.Trim(separator) + separator + validsecond.Trim(separator);
                }
                );
            return (IsRelative ? separator.ToString() : string.Empty) + final;
        }
    

    It works for URLs as well as normal paths.

    Usage:

        // Fixes internal paths
        Console.WriteLine(PathCombine(true , true , true , @"\/\/folder 1\/\/\/\\/\folder2\///folder3\\/" , @"/\somefile.ext\/\//\"));
        // Result: /folder 1/folder2/folder3/somefile.ext
    
        // Doesn't fix internal paths
        Console.WriteLine(PathCombine(true , true , false , @"\/\/folder 1\/\/\/\\/\folder2\///folder3\\/" , @"/\somefile.ext\/\//\"));
        //result : /folder 1//////////folder2////folder3/somefile.ext
    
        // Don't worry about URL prefixes when fixing internal paths
        Console.WriteLine(PathCombine(true , false , true , @"/\/\/https:/\/\/\lul.com\/\/\/\\/\folder2\///folder3\\/" , @"/\somefile.ext\/\//\"));
        // Result: https://lul.com/folder2/folder3/somefile.ext
    
        Console.WriteLine(PathCombine(false , true , true , @"../../../\\..\...\./../somepath" , @"anotherpath"));
        // Result: \..\..\..\..\...\.\..\somepath\anotherpath
    
    0 讨论(0)
  • 2020-11-22 15:25

    I found that the Uri constructor flips '\' into '/'. So you can also use Path.Combine, with the Uri constructor.

     Uri baseUri = new Uri("http://MyUrl.com");
     string path = Path.Combine("Images", "Image.jpg");
     Uri myUri = new Uri(baseUri, path);
    
    0 讨论(0)
  • 2020-11-22 15:26

    Path.Combine does not work for me because there can be characters like "|" in QueryString arguments and therefore the URL, which will result in an ArgumentException.

    I first tried the new Uri(Uri baseUri, string relativeUri) approach, which failed for me because of URIs like http://www.mediawiki.org/wiki/Special:SpecialPages:

    new Uri(new Uri("http://www.mediawiki.org/wiki/"), "Special:SpecialPages")
    

    will result in Special:SpecialPages, because of the colon after Special that denotes a scheme.

    So I finally had to take mdsharpe/Brian MacKays route and developed it a bit further to work with multiple URI parts:

    public static string CombineUri(params string[] uriParts)
    {
        string uri = string.Empty;
        if (uriParts != null && uriParts.Length > 0)
        {
            char[] trims = new char[] { '\\', '/' };
            uri = (uriParts[0] ?? string.Empty).TrimEnd(trims);
            for (int i = 1; i < uriParts.Length; i++)
            {
                uri = string.Format("{0}/{1}", uri.TrimEnd(trims), (uriParts[i] ?? string.Empty).TrimStart(trims));
            }
        }
        return uri;
    }
    

    Usage: CombineUri("http://www.mediawiki.org/", "wiki", "Special:SpecialPages")

    0 讨论(0)
提交回复
热议问题