0

We were trying to build the pagination model around the API that will return the current page URL , previous page URL , next page URL.

In our Controller method, we have few well defined Query Parameter and user can some Query parameter on his own which will be handled in the controller using

         HttpUtility.ParseQueryString(Request.RequestUri.Query)

While preparing the current page URL , previous page URL , next page URL we will be modifying the limit offset and rest all the query parameters which user has passed should be there.

Is there any way dynamically build the URL as we might not be knowing all the Query String that user has passed.

We are currently using UrlHelper like this

       var prevLink = offset > 0 ? urlHelper.Link(routeName, new {offset=(offset-limit)>=0? offset - limit:0,limit=limit }) : ""

we are unable to pass the other dynamic Query string parameters which user might have passed. Is there any way around it.

harish
  • 35
  • 5

1 Answers1

0

There is no out of the box method that will only replace one query param. Give this a try:

var qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
qs.Set("offset", newOffset);

var uriBuilder = new UriBuilder(Request.RequestUri);
uriBuilder.Query = qs.ToString();
var newUri = uriBuilder.Uri;

Courtesy of: Replace item in querystring

Sal
  • 5,129
  • 5
  • 27
  • 53
  • I had to make few tweaks ti it. But it gives the basic Idea on what to be done. Thanks – harish Aug 04 '17 at 18:06
  • Can you share the tweaked version in your question too? In case any one else needs it? – Sal Aug 04 '17 at 18:08
  • the tweaks were mainly related to the business logic not to way of modifying the querystring, – harish Aug 07 '17 at 18:06