Get url without querystring
C#asp.netC# Problem Overview
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?
C# Solutions
Solution 1 - C#
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: https://stackoverflow.com/questions/1188096/truncating-query-string-returning-clean-url-c-asp-net/1188180#1188180
Solution 2 - C#
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.
Solution 3 - C#
This is my solution:
Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);
Solution 4 - C#
Good answer also found here source of answer
Request.Url.GetLeftPart(UriPartial.Path)
Solution 5 - C#
Request.RawUrl.Split(new[] {'?'})[0];
Solution 6 - C#
My way:
new UriBuilder(url) { Query = string.Empty }.ToString()
or
new UriBuilder(url) { Query = string.Empty }.Uri
Solution 7 - C#
You can use Request.Url.AbsolutePath
to get the page name, and Request.Url.Authority
for the host name and port. I don't believe there is a built in property to give you exactly what you want, but you can combine them yourself.
Solution 8 - C#
Split() Variation
I just want to add this variation for reference. Urls are often strings and so it's simpler to use the Split()
method than Uri.GetLeftPart()
. And Split()
can also be made to work with relative, empty, and null values whereas Uri throws an exception. Additionally, Urls may also contain a hash such as /report.pdf#page=10
(which opens the pdf at a specific page).
The following method deals with all of these types of Urls:
var path = (url ?? "").Split('?', '#')[0];
Example Output:
-
null ---> empty
-
empty ---> empty
-
http://domain/page.html?q=100#page=2 ---> http://domain/page.html
-
page.html ---> page.html
-
page.html?q=100 ---> page.html
-
page.html?q=100#page=2 ---> page.html
-
page.html#hash ---> page.html
Solution 9 - C#
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
Solution 10 - C#
Here's an extension method using @Kolman's answer. It's marginally easier to remember to use Path() than GetLeftPart. You might want to rename Path to GetPath, at least until they add extension properties to C#.
Usage:
Uri uri = new Uri("http://www.somewhere.com?param1=foo¶m2=bar");
string path = uri.Path();
The class:
using System;
namespace YourProject.Extensions
{
public static class UriExtensions
{
public static string Path(this Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
return uri.GetLeftPart(UriPartial.Path);
}
}
}
Solution 11 - C#
Request.RawUrl.Split('?')[0]
Just for url name only !!
Solution 12 - C#
string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path = url.split('?')[0];
Solution 13 - C#
Solution for Silverlight:
string path = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
Solution 14 - C#
I've created a simple extension, as a few of the other answers threw null exceptions if there wasn't a QueryString
to start with:
public static string TrimQueryString(this string source)
{
if (string.IsNullOrEmpty(source))
return source;
var hasQueryString = source.IndexOf('?') != -1;
if (!hasQueryString)
return source;
var result = source.Substring(0, source.IndexOf('?'));
return result;
}
Usage:
var url = Request.Url?.AbsoluteUri.TrimQueryString()
Solution 15 - C#
simple example would be using substring like :
string your_url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path_you_want = your_url .Substring(0, your_url .IndexOf("?"));
Solution 16 - C#
var canonicallink = Request.Url.Scheme + "://" + Request.Url.Authority + Request.Url.AbsolutePath.ToString();
Solution 17 - C#
Try this:
urlString=Request.RawUrl.ToString.Substring(0, Request.RawUrl.ToString.IndexOf("?"))
from this: http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye you'll get this: mypage.aspx
Solution 18 - C#
this.Request.RawUrl.Substring(0, this.Request.RawUrl.IndexOf('?'))