Skip to content

.NET implementation of URI template spec (RFC6570)

License

Notifications You must be signed in to change notification settings

a7b0/uri-templates

Repository files navigation

Resta.UriTemplates

Build Status NuGet

.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.
  • Targets .NET Standard 2.0

Install

Install via NuGet package

Examples

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

Resolve a URI template using fluent interface:

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()
    .Literal("http://example.org/")
    .Simple(new VarSpec("area"))
    .Literal("/last-news")
    .Query("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().Bind("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.

License

Copyright 2013 Pavel Shkarin

MIT License

About

.NET implementation of URI template spec (RFC6570)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages