tags:

views:

266

answers:

1

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
+7  A: 

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

Scott Ivey