C# – Get url without querystring

asp.netc

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.aspx from it.

Can you tell me how can I get it?

Best Answer

Here's a simpler solution:

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

Borrowed from here: Truncating Query String & Returning Clean URL C# ASP.net