I have a System.Uri, e.g.,
var uri = new Uri("http://www.domain.com/foo/bar");
How do I get only the relative Uri out of it:
/foo/bar
To link pages using relative URL in HTML, use the <a> tag with href attribute. Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc.
The AbsolutePath property contains the path information that the server uses to resolve requests for information. Typically this is the path to the desired information on the server's file system, although it also can indicate the application or script the server must run to provide the information.
The AbsoluteUri property includes the entire URI stored in the Uri instance, including all fragments and query strings.
What you are looking for is either Uri.AbsolutePath or Uri.PathAndQuery. PathAndQuery will include the querystring (which you don't have in your question), while AbsolutePath will give you just the path.
Console.WriteLine(uri.PathAndQuery); // or Console.WriteLine(uri.AbsolutePath);
...outputs the following results for both...
/foo/bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With