Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get url without querystring

Tags:

c#

asp.net

People also ask

How do I hide QueryString?

If you want to hide the query string in the URL, you need to use POST to deliver the ID in the body of the request instead of the query string.

Is QueryString safe?

Yes. The entire text of an HTTPS session is secured by SSL. That includes the query and the headers.

What is request QueryString ()?

The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter).


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


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.


This is my solution:

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

Good answer also found here source of answer

Request.Url.GetLeftPart(UriPartial.Path)

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

My way:

new UriBuilder(url) { Query = string.Empty }.ToString()

or

new UriBuilder(url) { Query = string.Empty }.Uri