Changing the scheme of System.Uri

后端 未结 4 1425
逝去的感伤
逝去的感伤 2020-12-30 19:02

I\'m looking for canonical way of changing scheme of a given System.Uri instance with System.UriBuilder without crappy string manipulations and magic constants. Say I have

4条回答
  •  囚心锁ツ
    2020-12-30 19:29

    Another iteration on Good Night Nerd Pride's answer, as an extension:

    public static Uri RewriteHttps(this Uri originalUri)
    {
        return new UriBuilder(originalUri)
        {
            Scheme = Uri.UriSchemeHttps,
            Port = originalUri.IsDefaultPort ? -1 : originalUri.Port // -1 => default port for scheme
        }.Uri;
    }
    

提交回复
热议问题