2011-05-13 74 views
1

我有一個Uri對象 - 它的屬性會給我相對路徑嗎?我該如何解讀這個Uri文件的相對路徑。我在C#編碼。uri的相對路徑

+1

你跟asp.net工作?相對於當前頁面? – 2011-05-13 21:16:26

+0

是aps.net和相對於當前路徑 – amateur 2011-05-13 21:18:13

回答

6

使用Uri.MakeRelativeUri Method (System)

直接從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)); 

而且,如果你總是在尋找從域的根目錄的相對路徑,你也可以使用myUri.AbsolutePath

下面是一個Uri調試視圖的屏幕截圖,其中兩個示例的MakeRelativeUri位於底部,使用以下Uri ob jects

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