Path.Combine for URLs?

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

    I created this function that will make your life easier:

        /// 
        /// The ultimate Path combiner of all time
        /// 
        /// 
        /// 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.
        /// 
        /// Just adds the separator at the beginning
        /// Fix the paths from within (by removing duplicate separators and correcting the separators)
        /// The paths to combine
        /// the combined path
        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
    

提交回复
热议问题