using Microsoft.AspNetCore.Http.Extensions;
using System.Collections.Generic;
using Microsoft.AspNetCore.WebUtilities;
public static void Main()
Console.WriteLine("Hello World");
"https://xxx.com/xxdda/what?state=xd3",
"https://xxx.com/xxdda/what?state",
"https://xxx.com/xxdda/what?",
"https://xxx.com/xxdda/what",
"https://xxx.com/xxdda/",
var xx = SupplyStateParameter(url,"xd");
static private string SupplyStateParameter(string rawurl, string state)
var uri = new Uri(rawurl);
var baseUri = uri.GetComponents(UriComponents.Scheme | UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.UriEscaped);
var query = QueryHelpers.ParseQuery(uri.Query);
if (!query.ContainsKey(nameof(state)))
query[nameof(state)] = string.IsNullOrWhiteSpace(state) ? Guid.NewGuid().ToString() : state;
var items = query.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();
var qb = new QueryBuilder(items);
return baseUri + qb.ToQueryString();