Coder Perfect

Obtain a URL that is devoid of the querystring.

Problem

I have a URL that looks like this:

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

I’m looking for http://www.example.com/mypage.aspx.

Could you please tell me how I may obtain it?

Asked by Rocky Singh

Solution #1

Here’s an easier way to do it:

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

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

Answered by Johnny Oshika

Solution #2

System is available. 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);

Substring is another option.

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

EDIT: Added brillyfresh’s proposal in the comments to the first solution.

Answered by Josh

Solution #3

Here’s what I’d do:

Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);

Answered by Kolman

Solution #4

Here is another good source of information.

Request.Url.GetLeftPart(UriPartial.Path)

Answered by 3 revs, 2 users 86%

Solution #5

Request.RawUrl.Split(new[] {'?'})[0];

Answered by tyy

Post is based on https://stackoverflow.com/questions/4630249/get-url-without-querystring