@@ -35,7 +35,7 @@ public static string ToQueryString<TKey, TValue>(this IDictionary<TKey, TValue>?
3535
3636 if ( item . Value is string )
3737 {
38- qs = AddQueryString ( key , FormatValue ( item . Value ) , qs , options . ValueEncoder ) ;
38+ qs = AddQueryString ( key , FormatValue ( key , item . Value ) , qs , options . ValueEncoder ) ;
3939 continue ;
4040 }
4141
@@ -45,15 +45,23 @@ public static string ToQueryString<TKey, TValue>(this IDictionary<TKey, TValue>?
4545 qs = BuildCollectionQueryString ( key , values , qs , options ) ;
4646 break ;
4747 default :
48- qs = AddQueryString ( key , FormatValue ( item . Value ) , qs , options . ValueEncoder ) ;
48+ qs = AddQueryString ( key , FormatValue ( key , item . Value ) , qs , options . ValueEncoder ) ;
4949 break ;
5050 }
5151 }
5252
5353 return qs ;
5454
55- string FormatValue ( object value )
56- => options . ValueFormatter == null ? value . ToString ( ) ! : options . ValueFormatter ( value ) ;
55+ string FormatValue ( object param , object value )
56+ {
57+ if ( options . ValueFormatter != null )
58+ return options . ValueFormatter ( value ) ;
59+
60+ if ( options . ValuePerKeyFormatter != null && param is string && options . ValuePerKeyFormatter . TryGetValue ( ( param as string ) ! , out var transform ) )
61+ return transform ( value ) ;
62+
63+ return value . ToString ( ) ! ;
64+ }
5765 }
5866
5967 /// <summary>
@@ -76,13 +84,18 @@ private static string BuildCollectionQueryString(string key, IEnumerable values,
7684 ? key
7785 : options . CollectionKeyFormatter ( key ) ;
7886
87+ var collectionFormatter = options . CollectionItemFormatter ;
88+
89+ if ( collectionFormatter == null && options . CollectionValuePerKeyItemFormatter ? . TryGetValue ( key , out var formatter ) is true )
90+ collectionFormatter = formatter ;
91+
7992 switch ( options . CollectionMode )
8093 {
8194 case QueryStringCollectionMode . KeyPerValue :
8295 foreach ( var value in values )
8396 {
84- var valueStr = options . CollectionItemFormatter != null
85- ? options . CollectionItemFormatter ( value )
97+ var valueStr = collectionFormatter != null
98+ ? collectionFormatter ( value )
8699 : value . ToString ( ) ;
87100 qs = AddQueryString ( key , valueStr , qs , options . ValueEncoder ) ;
88101 }
@@ -91,8 +104,8 @@ private static string BuildCollectionQueryString(string key, IEnumerable values,
91104 var index = 0 ;
92105 foreach ( var value in values )
93106 {
94- var valueStr = options . CollectionItemFormatter != null
95- ? options . CollectionItemFormatter ( value )
107+ var valueStr = collectionFormatter != null
108+ ? collectionFormatter ( value )
96109 : value . ToString ( ) ;
97110 if ( index == 0 )
98111 qs = AddQueryString ( key , valueStr , qs , options . ValueEncoder ) ;
0 commit comments