简体   繁体   English

如何删除URL的最后一部分?

[英]How to remove the last part of url?

I have this url: https://uk.soccerway.com/national/italy/serie-a/20172018/regular-season/r42011/?ICID=TN_02_01_02 我有这个网址: https://uk.soccerway.com/national/italy/serie-a/20172018/regular-season/r42011/?ICID=TN_02_01_02 : https://uk.soccerway.com/national/italy/serie-a/20172018/regular-season/r42011/?ICID=TN_02_01_02 ICID https://uk.soccerway.com/national/italy/serie-a/20172018/regular-season/r42011/?ICID=TN_02_01_02

I want remove ?ICID=TN_02_01_02 , of couse this string is dynamic so I can't use .Replace() method, any idea? 我想删除?ICID=TN_02_01_02 ,因为这个字符串是动态的,所以我不能使用.Replace()方法,有什么想法吗?

Use Uri try create ( https://msdn.microsoft.com/en-us/library/ms131572(v=vs.110).aspx ) to create a Uri object. 使用Uri尝试创建( https://msdn.microsoft.com/zh-cn/library/ms131572(v=vs.110).aspx )创建一个Uri对象。

Then with that Uri object take the "Query" property which will contain the entire query string. 然后使用该Uri对象获取“ Query”属性,该属性将包含整个查询字符串。

And use "AbsolutePath" to get the URL without the query string. 并使用“ AbsolutePath”获取不含查询字符串的URL。

You can use the Uri class to parse the URL. 您可以使用Uri类来解析URL。 It allows you to get only up to a specific part using the GetLeftPart method: 它允许您使用GetLeftPart方法仅获取特定零件:

public static string GetUriWithoutQuery(string url)
{
    var uri = new Uri(url);
    return uri.GetLeftPart(UriPartial.Path);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM