Remove last segment of Request.Url

前端 未结 5 634
旧巷少年郎
旧巷少年郎 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: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;
    

提交回复
热议问题