【发布时间】:2014-01-20 14:19:39
【问题描述】:
我正在使用以下网址,我需要从网址中删除 ?$format=xml。有没有简单的方法来实现这一点?
Uri uri = new Uri("https://ldcorp:435/mtp/op/ota/ind/Customer/?$format=xml);
【问题讨论】:
我正在使用以下网址,我需要从网址中删除 ?$format=xml。有没有简单的方法来实现这一点?
Uri uri = new Uri("https://ldcorp:435/mtp/op/ota/ind/Customer/?$format=xml);
【问题讨论】:
也许使用简单的字符串方法:
uriString = uri.ToString();
int indexOfQuestionMark = uriString.IndexOf("?");
if(indexOfQuestionMark >= 0)
{
uri = new Uri(uriString.Substring(0, indexOfQuestionMark));
}
或使用Uri 类本身和string.Format:
string pathWithoutQuery = String.Format("{0}{1}{2}{3}", uri.Scheme,
uri.Scheme, Uri.SchemeDelimiter, uri.Authority, uri.AbsolutePath);
uri = new Uri(pathWithoutQuery);
【讨论】:
IndexOf 而不是LastIndexOf。第二种方法应该始终有效。但是,一个有效的 url 真的可以包含多个问号吗? always just to delete from the url $format=xml 表示您只想删除此 url 参数而不删除其他任何内容?