Get url without querystring

前端 未结 18 2061
后悔当初
后悔当初 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:45

    System.Uri.GetComponents, just specified components you want.

    Uri uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
    uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped);
    

    Output:

    http://www.example.com/mypage.aspx
    

提交回复
热议问题