【问题标题】:Get specific field from Uri从 Uri 获取特定字段
【发布时间】:2014-01-20 14:19:39
【问题描述】:

我正在使用以下网址,我需要从网址中删除 ?$format=xml。有没有简单的方法来实现这一点?

Uri uri = new Uri("https://ldcorp:435/mtp/op/ota/ind/Customer/?$format=xml);

【问题讨论】:

    标签: c# url uri


    【解决方案1】:

    也许使用简单的字符串方法:

    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);
    

    【讨论】:

    • 谢谢蒂姆,但链接路径可以更改并且不止一个?在 URL 中,我只需要找到 ?$format=xml 并将其从 URL 中删除
    • @JeanTehhe:对不起,我没听懂你的评论。
    • 嗨蒂姆,问题是我可以在 URL 中有多个问号,我不需要最后一个总是只是从 url $format=xml 中删除,此外 URL 路径可以改成有更多的斜线
    • @JeanTehhe:然后在第一种方法中使用IndexOf 而不是LastIndexOf。第二种方法应该始终有效。但是,一个有效的 url 真的可以包含多个问号吗? always just to delete from the url $format=xml 表示您只想删除此 url 参数而不删除其他任何内容?
    • 嗨 Tim 我使用第一个选项,假设我没有问号我得到了错误,我该如何避免它>
    猜你喜欢
    • 1970-01-01
    • 2019-07-24
    • 2020-12-17
    • 2018-09-04
    • 2021-07-22
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多