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?
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
uri.GetComponent( is another awesome method for getting parts of a Uri. I didn't know about these two until now!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.
substring method will give error if there's no Query string. Use string path = url.Substring(0, url.IndexOf("?") > 0? url.IndexOf("?") : url.Length); instead.Succinct solution:
Request.RawUrl.Split('?')[0];
RawUrl will get the URL before URL rewrites, so perhaps use AbsoluteUri? Request.Url.AbsoluteUri.Split('?')[0]My way:
new UriBuilder(url) { Query = string.Empty }.ToString()
or
new UriBuilder(url) { Query = string.Empty }.Uri
Uri.GetLeftPart. Latest version of NET Core (1.1) should have that method (cant confirm because i'm not on net core 1.1 for now)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.
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:
page.html ---> page.html
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);
}
}
}
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()
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