UriTemplateTable.MatchSingle(Uri) Metoda

Definicja

Próbuje dopasować kandydata Uri do .UriTemplateTable

public:
 UriTemplateMatch ^ MatchSingle(Uri ^ uri);
public UriTemplateMatch MatchSingle (Uri uri);
member this.MatchSingle : Uri -> UriTemplateMatch
Public Function MatchSingle (uri As Uri) As UriTemplateMatch

Parametry

uri
Uri

Identyfikator URI kandydata.

Zwraca

UriTemplateMatch

Pojedyncze UriTemplateMatch wystąpienie.

Przykłady

W poniższym przykładzie pokazano, jak wywołać metodę MatchSingle(Uri) .

Uri prefix = new Uri("http://localhost/");

//Create a series of templates
UriTemplate weatherByCity = new UriTemplate("weather/ state}/ city}");
UriTemplate weatherByCountry = new UriTemplate("weather/ country}/ village}");
UriTemplate weatherByState = new UriTemplate("weather/ state}");
UriTemplate traffic = new UriTemplate("traffic/*");
UriTemplate wildcard = new UriTemplate("*");

//Create a template table
UriTemplateTable table = new UriTemplateTable(prefix);
//Add each template to the table with some associated data
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(weatherByCity, "weatherByCity"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(weatherByCountry, "weatherByCountry"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(weatherByState, "weatherByState"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(traffic, "traffic"));

table.MakeReadOnly(true);

//Call MatchSingle to retrieve some match results:
Uri weatherInSeattle = new Uri("http://localhost/weather/Washington/Seattle");
UriTemplateMatch match = table.MatchSingle(weatherInSeattle);

Console.WriteLine("Matching template: {0}", match.Template.ToString());
Dim prefix As New Uri("http://localhost/")

' Create a series of templates
Dim weatherByCity As New UriTemplate("weather/ state}/ city}")
Dim weatherByCountry As New UriTemplate("weather/ country}/ village}")
Dim weatherByState As New UriTemplate("weather/ state}")
Dim traffic As New UriTemplate("traffic/*")
Dim wildcard = New UriTemplate("*")

' Create a template table
Dim table As New UriTemplateTable(prefix)
' Add each template to the table with some associated data
table.KeyValuePairs.Add(New KeyValuePair(Of UriTemplate, Object)(weatherByCity, "weatherByCity"))
table.KeyValuePairs.Add(New KeyValuePair(Of UriTemplate, Object)(weatherByCountry, "weatherByCountry"))
table.KeyValuePairs.Add(New KeyValuePair(Of UriTemplate, Object)(weatherByState, "weatherByState"))
table.KeyValuePairs.Add(New KeyValuePair(Of UriTemplate, Object)(traffic, "traffic"))

table.MakeReadOnly(True)

' Call MatchSingle to retrieve some match results:
Dim weatherInSeattle As New Uri("http://localhost/weather/Washington/Seattle")
Dim match As UriTemplateMatch
match = table.MatchSingle(weatherInSeattle)

Console.WriteLine("Matching template: {0}", match.Template.ToString())

Uwagi

Ta metoda zwraca co najwyżej jedno dopasowanie. W przypadku znalezienia UriTemplateMatchException więcej niż jednego dopasowania zostanie zgłoszony element .

Dotyczy