Get url without querystring

前端 未结 18 2057
后悔当初
后悔当初 2020-11-28 21:24

I have a URL like this:

http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye

I want to get http://www.example.com/mypage

18条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 21:59

    You can use 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);
    

    Or you can use substring

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

    EDIT: Modifying the first solution to reflect brillyfresh's suggestion in the comments.

提交回复
热议问题