I have a Uri object - what property of it will give me the relative path? Of how can I decipher the relati开发者_运维问答ve path the file with this Uri. I am coding in c#.
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/");
You can add a relative path to the Uri like below.
var Url = new Uri("/something/test", UriKind.Relative);
精彩评论