amateur
amateur

Reputation: 44605

relative path to uri

I have a Uri object - what property of it will give me the relative path? Of how can I decipher the relative path the file with this Uri. I am coding in c#.

Upvotes: 3

Views: 9364

Answers (2)

Rushabh Master
Rushabh Master

Reputation: 490

You can add a relative path to the Uri like below.

var Url = new Uri("/something/test", UriKind.Relative);

Upvotes: 3

MikeM
MikeM

Reputation: 27405

use the Uri.MakeRelativeUri Method (System)

straight from MSDN:

// Create a base Uri.
Uri address1 = new Uri("http://www.contoso.com/");

// Create a new Uri from a string.
Uri address2 = new Uri("http://www.contoso.com/index.htm?date=today"); 

// Determine the relative Uri.  
Console.WriteLine("The difference is {0}", address1.MakeRelativeUri(address2));

furthermore, if you are always looking for the relative path from the root of the domain you could also use myUri.AbsolutePath

Here's a screenie of the Uri debug view with two examples of MakeRelativeUri at the bottom using the following Uri objects

Uri myUri = new Uri("http://msdn.microsoft.com/en-us/library/system.uri.makerelativeuri.aspx#Y600");
Uri myHost = new Uri("http://msdn.microsoft.com/");
Uri myHost2 = new Uri("http://msdn.microsoft.com/en-us/");

Uri debug

Upvotes: 8

Related Questions