using System.Collections.Generic;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.AspNetCore.Http.Extensions;
public static void Main()
var urlWithExistingQuery = "http://joesauve.com?param1=something¶m2=somethingelse";
var urlWithoutExistingQuery = "http://joesauve.com";
var dirtyUrlWithExistingQuery = QueryHelpers.AddQueryString(urlWithExistingQuery, "zbidref", id.ToString());
var dirtyUrlWithoutExistingQuery = QueryHelpers.AddQueryString(urlWithoutExistingQuery, "zbidref", id.ToString());
System.Console.WriteLine(dirtyUrlWithExistingQuery);
System.Console.WriteLine(GetSanitizedUrlForUserNotififcation(dirtyUrlWithExistingQuery));
System.Console.WriteLine();
System.Console.WriteLine(dirtyUrlWithoutExistingQuery);
System.Console.WriteLine(GetSanitizedUrlForUserNotififcation(dirtyUrlWithoutExistingQuery));
static string GetSanitizedUrlForUserNotififcation(string url)
var baseUri = uri.GetComponents(UriComponents.Scheme | UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.UriEscaped).TrimEnd(new char[] { '/' });
var query = QueryHelpers.ParseNullableQuery(uri.Query);
var queryItems = query.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();
queryItems.RemoveAll(x => x.Key.ToLowerInvariant() == "zbidref".ToLowerInvariant());
var sanitizedUrl = baseUri + (new QueryBuilder(queryItems)).ToQueryString();