I use this extension to create an RouteUrl
with the current query-string appended to it. This works fine for query-strings which does not have the same key twice or more.
public static string CurrentQueryStringRouteUrl(this UrlHelper url, string routeName, RouteValueDictionary routeValues)
{
var context = url.RequestContext;
var combinedRouteValues = new RouteValueDictionary();
var queryString = context.HttpContext.Request.QueryString;
foreach (var key in queryString.AllKeys.Where(key => key != null))
{
combinedRouteValues[key] = queryString[key];
}
if (routeValues != null)
{
foreach (var routeValue in routeValues)
{
combinedRouteValues[routeValue.Key] = routeValue.Value;
}
}
return url.RouteUrl(routeName, combinedRouteValues);
}
When there is query-string keys of same name, e.g. ?id=1&id=2&id=3
, this is converted into ?id=1,2,3
using the method above. Is there any way to avoid that? I wish to keep the original query-string as I am binding these values on a model list.
I am aware I can create a custom model-binder to bind the comma separated string to string[]
(or int[]
in this example) but I wish to avoid this as far of consistence goes.