开发者

relative path to uri

开发者 https://www.devze.com 2023-03-06 06:51 出处:网络
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 M

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/");

relative path to uri


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

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

精彩评论

暂无评论...
验证码 换一张
取 消