Coder Perfect

Obtain a URL that is devoid of the querystring.


I have a URL that looks like this:

I’m looking for

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("");
string path = uri.GetLeftPart(UriPartial.Path);

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

Answered by Johnny Oshika

Solution #2

System is available. Uri

Uri url = new Uri("");
string path = String.Format("{0}{1}{2}{3}", url.Scheme, 
    Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);

Substring is another option.

string url = "";
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.


Answered by 3 revs, 2 users 86%

Solution #5

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

Answered by tyy

Post is based on