.NET implementation of the URI template spec (RFC6570):
- Supports up to level 4 template expressions
- Fluent API for manipulating URI templates
- Strong validation and error reporting
- Precompiled URI templates
- Partial resolve of URI templates
- It is passes all tests defined by the uritemplate-test suite.
Install via NuGet package:
PM> Install-Package Resta.UriTemplates
Resolve a URI template:
var template = new UriTemplate("http://example.org/{area}/news{?type,count}");
var uri = template.Resolve(new Dictionary<string, object>
{
{ "area", "world" },
{ "type", "actual" },
{ "count", "10" }
});
Assert.AreEqual("http://example.org/world/news?type=actual&count=10", uri);
Another way to resolve a URI template:
var template = new UriTemplate("http://example.org/{area}/news{?type}");
var uri = template.GetResolver()
.Bind("area", "world")
.Bind("type", new string[] { "it", "music", "art" } )
.Resolve();
Assert.AreEqual("http://example.org/world/news?type=it,music,art", uri);
Construct a URI template:
var template = new UriTemplateBuilder()
.Append("http://example.org/")
.Append(new VarSpec("area"))
.Append("/news")
.Append('?', new VarSpec("type"), new VarSpec("count"))
.Build();
Assert.AreEqual("http://example.org/{area}/news{?type,count}", template.ToString());
Partial resolve a URI template:
var template = new UriTemplate("http://example.org/{area}/news{?type,count}");
var partiallyResolved = template.GetResolver("count", "10").ResolveTemplate();
Assert.AreEqual("http://example.org/{area}/news?count=10{&type}", partiallyResolved.ToString());
NB! Partial resolve of expressions "default", "reserved" and "fragment" is not possible for multiple variables.
Copyright 2013 Pavel Shkarin