URL获取上级目录

时间:2022-04-22 12:13:56
      /// <summary>
        /// 获取上级目录
        /// </summary>
        /// <param name="url">完整Url</param>
        /// <returns></returns>
        public string GetParentDirectory(string url)
        {
            return GetParentDirectory(new Uri(url));
        }
        /// <summary>
        /// 获取上级目录
        /// </summary>
        /// <param name="uri">uri</param>
        /// <returns></returns>
        public string GetParentDirectory(Uri uri)
        {
            return uri.Scheme + "://" + uri.Host + string.Join("", new List<string>(uri.Segments).GetRange(0, uri.Segments.Length - 1));
        }