1

I am working on a standard paging component for my project. All controllers with paging are waiting for PageIndex parameter in URL.

So I want to generate URL based on the current URL except for the PageIndex parameter.

For example, I have filters for my internet store like Manufacturer and MaxPrice. A user opens the mystore.com/products?manufacturer=Apple&MaxPrice=999 link.

Then he wants to go to the 3 pages. So the 3-page link in my paging should have the mystore.com/products?manufacturer=Apple&MaxPrice=999&PageIndex=3 link.

enter image description here

So needed MVC function should:

  1. Persists all existing params like MaxPrice and manufacturer
  2. Replace only PageIndex param
  3. don't use any hardcoded controller and action values (like controller = 'Products', Action = 'Index')

I try to use this code:

<a class="page-link" href="@Url.RouteUrl(new { PageIndex = page })">
    @page
</a>

It works fine except the 2 rule - it doesn't persist other arguments. So if user click on this link he goes to mystore.com/products?PageIndex=3

6
  • you want to generate url bassed on current url, can you tell me why ? Commented Jan 6, 2019 at 13:15
  • Add a picture of what I want. Hope it helps. Yes, you right - I want to generate a new URL based on the current URL to persist other filter parameters (like Manufacturer and MaxPrice). In other words, I don't want to reset MaxPrice filter from URL after clicking on the 3 page of my paging (see picture). So the 3 page link should have the MaxPrice and Manufacturer params from original URL Commented Jan 6, 2019 at 13:23
  • perfect, I suggest to build the url dynamically by getting currentUrl with query strings "Request.Url.AbsoluteUri" then remove the pageIndex from url if exists , then add page index again, if you cannot understand let me know I can help. hint : url must be defined as variable in your razor to make the things easier. Commented Jan 6, 2019 at 13:32
  • I know how to do it manually (like you described), but it looks like a rough and ugly solution. I have thought that there is some standard solution in Asp.net Core MVC for such problem ( Commented Jan 6, 2019 at 13:36
  • You can make the things easy by creating extension method for to avoid adding complex code to your razor, I don't think that there is built in method to do your requirements Commented Jan 6, 2019 at 13:38

2 Answers 2

1

I suggest to build the url dynamically by getting currentUrl with query strings "Request.Url.AbsoluteUri" then remove the pageIndex from url if exists , then add page index again.

hint : url must be defined as variable in your razor to make the things easier

To remove query string you can use regex

string queryString = "Default.aspx?Agent=10&Language=2"; //Request.QueryString.ToString();
string parameterToRemove="Language";   //parameter which we want to remove
string regex=string.Format("(&{0}=[^&\s]+|{0}=[^&\s]+&?)",parameterToRemove);
string finalQS = Regex.Replace(queryString, regex, "");
Sign up to request clarification or add additional context in comments.

Comments

0

You can use these to get the current url

string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TEST/Default.aspx

string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TEST/Default.aspx

And then you can add the page index like this and redirect to that url

url = url+"&PageIndex=3";

Recommended

Or you can get the url parameters with

@Url.RequestContext.RouteData.Values["manufacturer"]
@Url.RequestContext.RouteData.Values["MaxPrice"]

And use those values to build the new URL inside the View

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.