代码之家  ›  专栏  ›  技术社区  ›  Rohit

从绝对名称C获取uri/url的父名称#

  •  16
  • Rohit  · 技术社区  · 16 年前

    给定一个绝对的uri/url,我想得到一个不包含叶部分的uri/url。例如:给定 http://foo.com/bar/baz.html 我应该得到 http://foo.com/bar/ .

    我能想到的代码似乎有点冗长,所以我想知道是否有更好的方法。

    static string GetParentUriString(Uri uri)
        {            
            StringBuilder parentName = new StringBuilder();
    
            // Append the scheme: http, ftp etc.
            parentName.Append(uri.Scheme);            
    
            // Appned the '://' after the http, ftp etc.
            parentName.Append("://");
    
            // Append the host name www.foo.com
            parentName.Append(uri.Host);
    
            // Append each segment except the last one. The last one is the
            // leaf and we will ignore it.
            for (int i = 0; i < uri.Segments.Length - 1; i++)
            {
                parentName.Append(uri.Segments[i]);
            }
            return parentName.ToString();
        }
    

    我们可以使用这样的函数:

      static void Main(string[] args)
        {            
            Uri uri = new Uri("http://foo.com/bar/baz.html");
            // Should return http://foo.com/bar/
            string parentName = GetParentUriString(uri);                        
        }
    

    谢谢, 罗希特

    10 回复  |  直到 6 年前
        1
  •  27
  •   Martin    16 年前

    这是我能想到的最短的:

    static string GetParentUriString(Uri uri)
    {
        return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.Last().Length);
    }
    

    如果要使用last()方法,则必须包含System.Linq。

        2
  •  29
  •   AeroX arpit desai    9 年前

    你试过这个吗?看起来很简单。

    Uri parent = new Uri(uri, "..");
    
        3
  •  7
  •   AeroX arpit desai    9 年前

    对于内置的URI方法,必须有一种更简单的方法来实现这一点,但下面是我对@unknown(yahoo)建议的转弯抹角。
    在这个版本中,您不需要 System.Linq 它还使用查询字符串处理URI:

    private static string GetParentUriString(Uri uri)
    {
        return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments[uri.Segments.Length -1].Length - uri.Query.Length);
    }
    
        4
  •  1
  •   dbkk    16 年前

    又快又脏

    int pos = uriString.LastIndexOf('/');
    if (pos > 0) { uriString = uriString.Substring(0, pos); } 
    
        5
  •  1
  •   riel    14 年前

    我找到的最短路径:

    static Uri GetParent(Uri uri) {
        return new Uri(uri, Path.GetDirectoryName(uri.LocalPath) + "/");
    }
    
        6
  •  1
  •   Andre Soares SLaks    8 年前

    我在这里读了很多答案,但没有找到我喜欢的答案,因为它们在某些情况下会断裂。

    所以,我用这个:

    public Uri GetParentUri(Uri uri) {
        var withoutQuery = new Uri(uri.GetComponents(UriComponents.Scheme |
                                                     UriComponents.UserInfo |
                                                     UriComponents.Host |
                                                     UriComponents.Port |
                                                     UriComponents.Path, UriFormat.UriEscaped));
        var trimmed = new Uri(withoutQuery.AbsoluteUri.TrimEnd('/'));
        var result = new Uri(trimmed, ".");
        return result;
    }
    

    注: 它故意删除查询和片段。

        7
  •  0
  •   stung    13 年前
    new Uri(uri.AbsoluteUri + "/../")
    
        8
  •  0
  •   Sport    9 年前

    获取URL段

    url="http://localhost:9572/School/Common/Admin/Default.aspx"
    
    Dim name() As String = HttpContext.Current.Request.Url.Segments
    
    now simply using for loop or by index, get parent directory name
    
    code = name(2).Remove(name(2).IndexOf("/"))
    

    这让我“普通”

        9
  •  0
  •   AeroX arpit desai    9 年前

    Papyref的回答不正确, UriPartial.Path 包括文件名。

    new Uri(uri, ".").ToString()
    

    似乎是所请求函数的最干净/最简单的实现。

        10
  •  0
  •   hector-j-rivas    6 年前

    我想我会插话的;尽管已经有近10年了,随着云技术的出现,获取父URI是一个相当常见(而且imo更具价值)的场景,因此结合这里的一些答案,您只需使用(扩展)URI语义:

    public static Uri Parent(this Uri uri)
    {
        return new Uri(uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.Last().Length - uri.Query.Length).TrimEnd('/'));
    }
    
    var source = new Uri("https://foo.azure.com/bar/source/baz.html?q=1");
    
    var parent = source.Parent();         // https://foo.azure.com/bar/source
    var folder = parent.Segments.Last();  // source
    

    我不能说我已经测试了所有的场景,所以谨慎建议。