Change Single URL query string value

前端 未结 5 482
说谎
说谎 2020-12-01 02:08

I have an ASP.NET page which takes a number of parameters in the query string:

search.aspx?q=123&source=WebSearch

This would display th

5条回答
  •  心在旅途
    2020-12-01 03:01

    This is pretty arbitrary, in .NET Core at least. And it all boils down to asp-all-route-data

    Consider the following trivial example (taken from the "paginator" view model I use in virtually every project):

    public class SomeViewModel 
    {
    
        public Dictionary NextPageLink(IQueryCollection query)
        {
            /*
             * NOTE: how you derive the "2" is fully up to you
             */
            return ParseQueryCollection(query, "page", "2");
        }
    
        Dictionary ParseQueryCollection(IQueryCollection query, string replacementKey, string replacementValue)
        {
            var dict = new Dictionary()
            {
                { replacementKey, replacementValue }
            };
    
            foreach (var q in query)
            {
                if (!string.Equals(q.Key, replacementKey, StringComparison.OrdinalIgnoreCase))
                {
                    dict.Add(q.Key, q.Value);
                }
            }
    
            return dict;
        }
    }
    

    Then to use in your view, simply pass the method the current request query collection from Context.Request:

    Next
    

提交回复
热议问题