Package Introduction-Uritemplates (for working with formatted URI templates)

Source: Internet
Author: User

Uritemplates for working with formatted URI templates

Pm> Install-package Tavis.uritemplates

Set URI Path Segment

[fact]public void Updatepathparameter () {    var url = new UriTemplate ("Http://example.org/{tenant}/customers")        . Addparameter ("tenant", "Acmé")        . Resolve ();    Assert.equal ("http://example.org/acm%C3%A9/customers", url);}

Set QueryString

[fact]public void Shouldresolveuritemplatewithnonstringparameter () {    var url = new UriTemplate ("HTTP/ EXAMPLE.ORG/LOCATION{?LAT,LNG} ")        . AddParameters (New {lat = 31.464, LNG = 74.386})        . Resolve ();    Assert.equal ("http://example.org/location?lat=31.464&lng=74.386", url);}

If the parameter is not set the URL will delete this parameter

[fact]public void Someparametersfromanobject () {    var url = new UriTemplate ("http://example.org{/environment}{/ Version}/customers{?active,country} ")        . AddParameters (new        {            Version = "V2",            active = "true"        })        . Resolve ();    Assert.equal ("http://example.org/v2/customers?active=true", url);}

  

You can pass a list as a parameter

[fact]public void Applyparametersobjectwithalistofints () {    var url = new UriTemplate ("http://example.org/ Customers{?ids,order} ")        . AddParameters (new        {            order = "Up",            ids = new[] {+,-        )})        . Resolve ();    Assert.equal ("http://example.org/customers?ids=21,75,21&order=up", url);}

Add a dictionary as a parameter

[fact]public void Applydictionarytoqueryparameters () {    var url = new UriTemplate ("http://example.org/foo{?coords* }")        . Addparameter ("Coords", New dictionary<string, string>        {            {"x", "1"},            {"Y", "2"},        })        . Resolve ();    Assert.equal ("http://example.org/foo?x=1&y=2", url);}

Package Introduction-Uritemplates (for working with formatted URI templates)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.