/// <summary> /// Tries to create a safe <see cref="Uri"/> object from a string. /// </summary> /// <param name="url">The URL.</param> /// <returns>The <see cref="Uri"/> object or null if no safe <see cref="Uri"/> can be created.</returns> protected Uri GetSafeUri(string url) { Uri uri; if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uri) || !uri.IsAbsoluteUri && !IsWellFormedRelativeUri(uri) || uri.IsAbsoluteUri && !AllowedSchemes.Contains(uri.Scheme, StringComparer.OrdinalIgnoreCase)) { return(null); } return(uri); }
/// <summary> /// Tries to create a safe <see cref="Iri"/> object from a string. /// </summary> /// <param name="url">The URL.</param> /// <returns>The <see cref="Iri"/> object or null if no safe <see cref="Iri"/> can be created.</returns> protected Iri GetSafeIri(string url) { var schemeMatch = SchemeRegex.Match(url); if (schemeMatch.Success) { var scheme = schemeMatch.Groups[1].Value; return(AllowedSchemes.Contains(scheme, StringComparer.OrdinalIgnoreCase) ? new Iri { Value = url, Scheme = scheme } : null); } return(new Iri { Value = url }); }