Can this code be improved? I'm avoiding using the QueryString collection because the query string may contain eg: OrderBy=Column1&OrderBy=Column2
and it didn't seem helpful to start with those switched into QueryString["OrderBy"]=="Column1,Column2";
var query = "";
if (urlHelper.RequestContext.HttpContext.Request.Url != null)
{
query = urlHelper.RequestContext.HttpContext.Request.Url.Query.TrimStart('?');
if (query.Length > 0)
{
var kvp =
query.Split('&').Select(x => x.Split('='))
.Select(x => new { Key = x[0], Value = (x.Length > 0) ? x[1] : ""})
.Where(x => x.Key != "page")
.ToList();
kvp.Add(new { Key = "page", Value = number.ToString() });
query = String.Join("&", kvp.Select(x => x.Key + "=" + x.Value).ToArray());
}
else query = String.Format("page={0}", number);
}
QueryString["OrderBy"]=="Column1,Column2"
. It seems to be more regular structure than several parameters with the same name. – Snowbear Jun 1 '11 at 16:39