【问题标题】:PowerShell URI template parsingPowerShell URI 模板解析
【发布时间】:2019-11-18 21:53:41
【问题描述】:
【问题讨论】:
标签:
.net
powershell
rest
octopus-deploy
hateoas
【解决方案1】:
所以我找不到任何原生解决方案,但我将分享一个实现 Resta.UriTemplates 的示例。
# Example adapted from: https://github.com/a7b0/uri-templates#examples
Install-Package Resta.UriTemplates -Version 1.3.0
# Import the Resta.UriTemplates assembly
$Path = '.\Resta.UriTemplates.1.3.0\lib\netstandard2.0\Resta.UriTemplates.dll'
Add-Type -Path $Path -PassThru | Select-Object -ExpandProperty Assembly | Select-Object -ExpandProperty FullName -Unique
# Returns:
# Resta.UriTemplates, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null
$template = [Resta.UriTemplates.UriTemplate]'http://example.org/{area}/news{?type,count}'
Write-Output $template
# Returns:
# Template Variables
# -------- ---------
# http://example.org/{area}/news{?type,count} {area, type, count}
$dict = New-Object 'system.collections.generic.dictionary[string,object]'
$dict["area"] = "world"
$dict["type"] = "actual"
$dict["count"] = "10"
$template.Resolve($dict)
# Returns:
# http://example.org/world/news?type=actual&count=10