Remove last segment of Request.Url

前端 未结 5 633
旧巷少年郎
旧巷少年郎 2021-01-07 22:59

I would like to remove the last segment of Request.Url, so for instance...

http://www.example.com/admin/users.aspx/deleteUser

相关标签:
5条回答
  • 2021-01-07 23:40

    To remove the last segment of Request.Url it is enough to subtract from absolute uri the length of last segment.

    string uriWithoutLastSegment = Request.Url.AbsoluteUri.Remove(
      Request.Url.AbsoluteUri.Length - Request.Url.Segments.Last().Length );
    
    0 讨论(0)
  • 2021-01-07 23:44

    Use the Uri class to parse the URI - you can access all the segments using the Segments property and rebuild the URI without the last segment.

    var uri = new Uri(myString);
    
    var noLastSegment = string.Format("{0}://{1}", uri.Scheme, uri.Authority);
    
    for(int i = 0; i < uri.Segments.Length - 1; i++)
    {
       noLastSegment += uri.Segments[i];
    }
    
    noLastSegment = noLastSegment.Trim("/".ToCharArray()); // remove trailing `/`
    

    As an alternative to getting the scheme and host name, as suggested by Dour High Arch in his comment:

    var noLastSegment = uri.GetComponents(UriComponents.SchemeAndServer, 
                                          UriFormat.SafeUnescaped);
    
    0 讨论(0)
  • 2021-01-07 23:50

    Much the same as @Oded's answer, but using a UriBuilder instead:

    var uri = new Uri("http://www.example.com/admin/users.aspx/deleteUser");
    var newSegments = uri.Segments.Take(uri.Segments.Length - 1).ToArray();
    newSegments[newSegments.Length-1] = 
        newSegments[newSegments.Length-1].TrimEnd('/');
    var ub=new UriBuilder(uri);
    ub.Path=string.Concat(newSegments);
    //ub.Query=string.Empty;  //maybe?
    var newUri=ub.Uri;
    
    0 讨论(0)
  • 2021-01-07 23:58

    I find manipulating Uri's fairly annoying, and as the other answers are quite verbose, here's my two cents in the form of an extension method.

    As a bonus you also get a replace last segement method. Both methods will leave querystring and other parts of the url intact.

    public static class UriExtensions
    {
        private static readonly Regex LastSegmentPattern = 
            new Regex(@"([^:]+://[^?]+)(/[^/?#]+)(.*$)", RegexOptions.Compiled | RegexOptions.IgnoreCase);        
    
        public static Uri ReplaceLastSegement(this Uri me, string replacement)
            => me != null ? new Uri(LastSegmentPattern.Replace(me.AbsoluteUri, $"$1/{replacement}$3")) : null;
    
        public static Uri RemoveLastSegement(this Uri me)
            => me != null ? new Uri(LastSegmentPattern.Replace(me.AbsoluteUri, "$1$3")) : null;
    }
    
    0 讨论(0)
  • 2021-01-07 23:59

    Well the trivial solution would be to iterate char by char from the end of the string towards its beginning and search for the first '/' to come (I guess that also came into your mind).

    Try this:

    string url = "http://www.example.com/admin/users.aspx/deleteUser";
    
    for (int i = url.Length - 1; i >= 0; i--) {
    
        if (url[i] == '/') return url.Substring(0, i - 1);
    }
    
    0 讨论(0)
提交回复
热议问题