Add scheme to URL if needed

后端 未结 5 1915
耶瑟儿~
耶瑟儿~ 2020-12-08 08:49

To create a Uri from a string you can do this:

Uri u = new Uri(\"example.com\");

But the problem is if the string (like the one above) does

5条回答
  •  不思量自难忘°
    2020-12-08 09:46

    You could also use UriBuilder:

    public static Uri GetUri(this string s)
    {
        return new UriBuilder(s).Uri;
    }
    

    Remarks from MSDN:

    This constructor initializes a new instance of the UriBuilder class with the Fragment, Host, Path, Port, Query, Scheme, and Uri properties set as specified in uri.

    If uri does not specify a scheme, the scheme defaults to "http:".

提交回复
热议问题