比较两个URI可以使用
System.Uri.Compare完成.
但是,将http://example.com/pages?pageStart=100\u0026amp;pageSize=50与http://example.com/pages?pageSize=50\u0026amp;pageStart=100进行比较表示两个URI不同.有没有办法让比较忽略值在查询字符串中出现的顺序;从功能上讲,这两个URI是相同的.
我正在考虑编写一个包装器方法,在调用标准比较函数之前按顺序放置查询字符串参数,但是想先检查一下是否有开箱即用的解决方案,因为比较不会显得很奇怪已经有了这个选项.
更新
这是我提出的用于比较两个URI的解决方案,无论它们的查询字符串参数顺序如何.如上所述,这是通过首先对每个URI的查询字符串进行排序来实现的.但是,如果.net中存在现有解决方案,我宁可放弃我的代码和放大器.用那个.
function CompareUri ($GivenUri, $ShouldBeUri) {
$uriComponentsOptions = ([UriComponents]::AbsoluteUri)
$uriFormatOptions = ([UriFormat]::SafeUnescaped)
$stringComparisonOptions = ([StringComparison]::OrdinalIgnoreCase)
$a = OrderUriQueryString($GivenUri)
$b = OrderUriQueryString($ShouldBeUri)
[Uri]::Compare($a, $b, $uriComponentsOptions, $uriFormatOptions, $stringComparisonOptions)
}
function OrderUriQueryString($Uri) {
[System.UriBuilder]$UriBuilder = New-Object -TypeName 'System.UriBuilder' -ArgumentList $Uri
[System.Collections.Specialized.NameValueCollection]$Query = [System.Web.HttpUtility]::ParseQueryString($UriBuilder.Query)
[System.Collections.Specialized.NameValueCollection]$Query2 = [System.Web.HttpUtility]::ParseQueryString('') #we have to initialise this way as HttpValueCollection has no public constructor (https://referencesource.microsoft.com/#system.web/HttpValueCollection.cs,fde6b9ec5f1ed58a,references)
$Query.AllKeys | sort | %{ $Query2.Add($_, $Query[$_]) }
$UriBuilder.Query = $Query2.ToString()
$UriBuilder.ToString()
}
最佳答案 我知道这与你做的基本相同,但是如果你真的只是希望比较在代码中看起来更简洁,你可以使用Update-TypeData修改System.Uri类型并使用它而不是两个单独的函数.它不是原生的.NET,基本上使用的是相同的代码,但至少比较的调用看起来更清晰.
Update-TypeData -MemberType "ScriptMethod" -MemberName "SortedQueryStringCompare" -TypeName "System.Uri" -ErrorAction "SilentlyContinue" -Value {
Param (
[System.URI]$ComparingURI
)
$Sorter = {
$UriBuilder = [System.UriBuilder]$args[0]
$UnsortedQuery = [System.Web.HttpUtility]::ParseQueryString($UriBuilder.Query)
$SortedQuery = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
$UnsortedQuery.AllKeys | Sort-Object | ForEach-Object {
$SortedQuery.Add($_, $UnsortedQuery[$_])
}
$UriBuilder.Query = $SortedQuery.ToString()
return $UriBuilder.ToString()
}
$UriComponentsOptions = ([UriComponents]::AbsoluteUri)
$UriFormatOptions = ([UriFormat]::SafeUnescaped)
$StringComparisonOptions = ([StringComparison]::OrdinalIgnoreCase)
$OriginUriString = . $Sorter $This
$ComparingUriString = . $Sorter $ComparingURI
return [System.Uri]::Compare($OriginUriString,
$ComparingUriString,
$UriComponentsOptions,
$UriFormatOptions,
$StringComparisonOptions)
}
$e1 = [System.Uri]"http://example.com/pages?pageStart=100&pageSize=50"
$e2 = [System.Uri]"http://example.com/pages?pageSize=50&pageStart=100"
$e1.SortedQueryStringCompare($e2)
或者只是使用SortQueryString方法执行此操作,并将其用于比较调用
Update-TypeData -MemberType "ScriptMethod" -MemberName "SortQueryString" -TypeName "System.Uri" -ErrorAction "SilentlyContinue" -Value {
$UriBuilder = [System.UriBuilder]$this
$UnsortedQuery = [System.Web.HttpUtility]::ParseQueryString($UriBuilder.Query)
$SortedQuery = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
$UnsortedQuery.AllKeys | Sort-Object | ForEach-Object {
$SortedQuery.Add($_, $UnsortedQuery[$_])
}
$UriBuilder.Query = $SortedQuery.ToString()
return [System.Uri]$UriBuilder.ToString()
}
$UriComponentsOptions = ([UriComponents]::AbsoluteUri)
$UriFormatOptions = ([UriFormat]::SafeUnescaped)
$StringComparisonOptions = ([StringComparison]::OrdinalIgnoreCase)
$e1 = [System.Uri]"http://example.com/pages?pageStart=100&pageSize=50"
$e2 = [System.Uri]"http://example.com/pages?pageSize=50&pageStart=100"
[System.Uri]::Compare($e1.SortQueryString(),
$e2.SortQueryString(),
$UriComponentsOptions,
$UriFormatOptions,
$StringComparisonOptions)