C#, Is there a better way to verify URL formatting than IsWellFormedUriString?

后端 未结 4 1891
夕颜
夕颜 2021-02-08 05:09

Is there a better/more accurate/stricter method/way to find out if a URL is properly formatted?

Using:

bool IsGoodUrl = Uri.IsWellForm         


        
4条回答
  •  情歌与酒
    2021-02-08 05:41

    @Greg's solution is correct. However you can steel using URI and validate all protocols (scheme) that you want as valid.

    public static bool Url(string p_strValue)
    {
        if (Uri.IsWellFormedUriString(p_strValue, UriKind.RelativeOrAbsolute))
        {
            Uri l_strUri = new Uri(p_strValue);
            return (l_strUri.Scheme == Uri.UriSchemeHttp || l_strUri.Scheme == Uri.UriSchemeHttps);
        }
        else
        {
            return false;
        }
    }
    

提交回复
热议问题