首页 / 问答 / C#/.NET/.NET Core应用程序编程开发中如何去掉URL字符串中的参数部分?

C#/.NET/.NET Core应用程序编程开发中如何去掉URL字符串中的参数部分?

.NET C# .NET Core 1.77K 次浏览
0

在C#/.NET/.NET Core应用程序编程开发中,有一个URL字符串,其中包含了一些URL参数,如:http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye,现需要将这个URL中的参数部分去掉,只保留参数前面的协议+主机部分:http://www.example.com/mypage.aspx,应该如何实现呢?

回复 [×]
提交评论
请输入评论内容

3 个回答

  • 0

    在C#/.NET/.NET Core中,可以使用System.Uri命名空间,方法如下:

    Uri url = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
    string path = String.Format("{0}{1}{2}{3}", url.Scheme,
        Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);
    
    Rector的个人主页

    Rector

    2020-05-06 回答

    • 0

      使用字符串截取'url'.Substring(),如下:

      string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
      string path = url.Substring(0, url.IndexOf("?"));
      
      Rector的个人主页

      Rector

      2020-05-06 回答

      • 0

        使用Uri.GetLeftPart()方法,如下:

        var uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
        string path = uri.GetLeftPart(UriPartial.Path);
        
        Rector的个人主页

        Rector

        2020-05-06 回答

        我来回答