/// <summary> /// Sanitizes a URL. /// </summary> /// <param name="url">The URL.</param> /// <param name="baseUrl">The base URL relative URLs are resolved against (empty or null for no resolution).</param> /// <returns>The sanitized URL or null if no safe URL can be created.</returns> protected virtual string SanitizeUrl(string url, string baseUrl) { var iri = GetSafeIri(url); if (iri != null && !iri.IsAbsolute && !string.IsNullOrEmpty(baseUrl)) { // resolve relative uri if (Uri.TryCreate(baseUrl, UriKind.Absolute, out Uri baseUri)) { try { return(new Uri(baseUri, iri.Value).AbsoluteUri); } catch (UriFormatException) { iri = null; } } else { iri = null; } } var e = new FilterUrlEventArgs { OriginalUrl = url, SanitizedUrl = iri?.Value }; OnFilteringUrl(e); return(e.SanitizedUrl); }
/// <summary> /// Raises the <see cref="E:RemovingUrl" /> event. /// </summary> /// <param name="e">The <see cref="FilterUrlEventArgs"/> instance containing the event data.</param> protected virtual void OnFilteringUrl(FilterUrlEventArgs e) { FilterUrl?.Invoke(this, e); }