C# 删除 Request.Url 的最后一段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11529326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove last segment of Request.Url
提问by bflemi3
I would like to remove the last segment of Request.Url, so for instance...
我想删除 的最后一段Request.Url,例如...
http://www.example.com/admin/users.aspx/deleteUser
would change to
会变成
http://www.example.com/admin/users.aspx
I would prefer linq but accept any solution that efficiently works.
我更喜欢 linq 但接受任何有效的解决方案。
采纳答案by Oded
Use the Uriclass to parse the URI - you can access all the segments using the Segmentsproperty and rebuild the URI without the last segment.
使用Uri该类来解析 URI - 您可以使用该Segments属性访问所有段,并在没有最后一段的情况下重建 URI。
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:
作为获取方案和主机名的替代方法,正如 Dour High Arch 在他的评论中所建议的:
var noLastSegment = uri.GetComponents(UriComponents.SchemeAndServer,
UriFormat.SafeUnescaped);
回答by marc wellman
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);
}
回答by spender
Much the same as @Oded's answer, but using a UriBuilder instead:
与@Oded 的答案非常相似,但使用 UriBuilder 代替:
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;
回答by Mentor
To remove the last segment of Request.Url it is enough to subtract from absolute uri the length of last segment.
要删除 Request.Url 的最后一段,从绝对 uri 中减去最后一段的长度就足够了。
string uriWithoutLastSegment = Request.Url.AbsoluteUri.Remove(
Request.Url.AbsoluteUri.Length - Request.Url.Segments.Last().Length );
回答by Dimitri Troncquo
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.
我发现操纵 Uri 相当烦人,并且由于其他答案非常冗长,这里是我的扩展方法形式的两分钱。
As a bonus you also get a replace last segement method. Both methods will leave querystring and other parts of the url intact.
作为奖励,您还可以获得替换最后一段方法。这两种方法都会使查询字符串和 url 的其他部分保持不变。
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, $"/{replacement}")) : null;
public static Uri RemoveLastSegement(this Uri me)
=> me != null ? new Uri(LastSegmentPattern.Replace(me.AbsoluteUri, "")) : null;
}

