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);
}
}
}
this.Request.RawUrl.Substring(0, this.Request.RawUrl.IndexOf('?'))
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.
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
Request.RawUrl.Split(new[] {'?'})[0];
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()
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
var canonicallink = Request.Url.Scheme + "://" + Request.Url.Authority + Request.Url.AbsolutePath.ToString();
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
Good answer also found here source of answer
Request.Url.GetLeftPart(UriPartial.Path)
Request.RawUrl.Split('?')[0]
Just for url name only !!
Solution for Silverlight:
string path = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
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
My way:
new UriBuilder(url) { Query = string.Empty }.ToString()
or
new UriBuilder(url) { Query = string.Empty }.Uri
This is my solution:
Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);
string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path = url.split('?')[0];
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("?"));
Source: Stackoverflow.com