Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the relative uri from the absolute uri

Tags:

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 
like image 727
Jim Avatar asked Jul 10 '09 13:07

Jim


People also ask

How do I get the relative URL in HTML?

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.

What is Uri absolute path?

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.

What is absolute URI in C#?

The AbsoluteUri property includes the entire URI stored in the Uri instance, including all fragments and query strings.


1 Answers

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

like image 193
Scott Ivey Avatar answered Oct 01 '22 02:10

Scott Ivey