共用方式為


Uri.MakeRelativeUri Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Determines the difference between two Uri instances.

Namespace:  System
Assembly:  System (in System.dll)

Syntax

'Declaration
Public Function MakeRelativeUri ( _
    uri As Uri _
) As Uri
public Uri MakeRelativeUri(
    Uri uri
)

Parameters

  • uri
    Type: System.Uri
    The URI to compare to the current URI.

Return Value

Type: System.Uri
If the hostname and scheme of this URI instance and toUri are the same, then this method returns a relative Uri that, when appended to the current URI instance, yields toUri.
If the hostname or scheme is different, then this method returns a Uri that represents the toUri parameter.

Exceptions

Exception Condition
InvalidOperationException

This instance represents a relative URI, and this property is valid only for absolute URIs.

Remarks

The following table shows the URI instance, toUri, and the results of calling MakeRelativeUri.

Current URI instance

toUri

Return value

https://www.contoso.com/

https://www.contoso.com/test/test.htm

test/test.htm

https://www.contoso.com/test1/

https://www.contoso.com/

../

https://www.contoso.com:8000/

https://www.contoso.com/test/test.htm

https://www.contoso.com/test/test.htm

https://username@www.contoso.com/

https://www.contoso.com/test1/test1.txt

test1/test1.txt

User information, if present in the URI, is ignored.

Examples

The following example creates 2 Uri instances. The difference in the path information is written to the console.

' Create a base Uri.
Dim address12 As Uri = New Uri("https://www.contoso.com/")

' Create a new Uri from a string.
Dim address22 As Uri = New Uri("https://www.contoso.com/index.htm?date=today")

' Determine the relative Uri.  
outputBlock.Text &= "The difference is "
outputBlock.Text &= address12.MakeRelativeUri(address22).ToString()
outputBlock.Text &= vbCrLf
// Create a base Uri.
Uri address12 = new Uri("https://www.contoso.com/");

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

// Determine the relative Uri.  
outputBlock.Text += "The difference is ";
outputBlock.Text += address12.MakeRelativeUri(address22);
outputBlock.Text += "\n";
// Create a base Uri.
Uri address1 = new Uri("https://www.contoso.com/");

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

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference