Exemplo n.º 1
0
        /// <summary>
        /// Transforms a relative path to absolute
        /// </summary>
        /// <param name="basePath">The base path</param>
        /// <param name="relativePath">The relative path</param>
        public string ResolveRelativePath(string basePath, string relativePath)
        {
            if (basePath == null)
            {
                throw new ArgumentNullException(
                          nameof(basePath),
                          string.Format(Strings.Common_ArgumentIsNull, nameof(basePath))
                          );
            }

            if (relativePath == null)
            {
                throw new ArgumentNullException(
                          nameof(relativePath),
                          string.Format(Strings.Common_ArgumentIsNull, nameof(relativePath))
                          );
            }

            // Convert backslashes to forward slashes
            string processedBasePath     = UrlHelpers.ProcessBackSlashes(basePath);
            string processedRelativePath = UrlHelpers.ProcessBackSlashes(relativePath);

            // Trying to convert paths to absolute paths
            string absoluteRelativePath;

            if (TryConvertToAbsolutePath(processedRelativePath, out absoluteRelativePath))
            {
                return(UrlHelpers.Normalize(absoluteRelativePath));
            }

            string absoluteBasePath;

            if (TryConvertToAbsolutePath(processedBasePath, out absoluteBasePath))
            {
                processedBasePath = absoluteBasePath;
            }

            if (string.IsNullOrWhiteSpace(processedBasePath))
            {
                return(UrlHelpers.Normalize(processedRelativePath));
            }

            if (string.IsNullOrWhiteSpace(processedRelativePath))
            {
                return(UrlHelpers.Normalize(processedBasePath));
            }

            string baseDirectoryName = UrlHelpers.GetDirectoryName(processedBasePath);
            string absolutePath      = UrlHelpers.Combine(baseDirectoryName, processedRelativePath);

            return(absolutePath);
        }