Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:06:31+00:00 2026-05-28T06:06:31+00:00

I have a partial view for displaying a list of items, I use this

  • 0

I have a partial view for displaying a list of items, I use this partial view in several different places. Inside this partial view I use a paginator –

@Html.PagedListPager(Model, page => Url.Action(null, new { page = page }))

This results in the paginator showing page urls for whatever Action and View i’m already looking at.

Problem is, on my search page I use a query string for the search string, and the Url.Action method does not include existing querystring parameters.

Instead of /Search?s=bla&page=3 I end up with /Search?page=3

How can I generate a url using the existing query string?

Edit:

Here is my code

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


        routes.Add(
        "Search",
        new SearchRoute("Search", new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(
                new { controller = "Search", action = "Index" })
        }); 


        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Call", action = "Index", id = UrlParameter.Optional }


        );



    }

    public class SearchRoute : Route
    {
        public SearchRoute(string url, IRouteHandler routeHandler)
            : base(url, routeHandler)
        {
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {

            System.Diagnostics.Debug.WriteLine(Url);

            if (HttpContext.Current != null)
            {

                string s = HttpContext.Current.Request.QueryString["s"];

                if (!string.IsNullOrEmpty(s))
                    values.Add("s", s);

            }

            return base.GetVirtualPath(requestContext, values);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:06:32+00:00Added an answer on May 28, 2026 at 6:06 am

    Using a custom route, you can preserve the query string because most Url generation logic uses the route to generate the URL. In this case I am checking the Request object for a query string called XXX and adding it into the route if it exists, you can make it more generic if you like.

    using System.Web;
    using System.Web.Routing;
    
    public class PreserveQueryStringRoute : Route
    {
        public PreserveQueryStringRoute(string url, IRouteHandler routeHandler)
            : base(url, routeHandler)
        {
        }
    
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            if(HttpContext.Current != null)
            {
                values = new RouteValueDictionary(values);   //this is the bug fix!
    
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["XXX"]))
                    values.Add("XXX", HttpContext.Current.Request.QueryString["XXX"]);
    
            }
    
            var path = base.GetVirtualPath(requestContext, values);
            return path;
        }
    }
    

    Register the route as per normal in the global.ascx (though probably not for such a generic match as I have below)

            routes.Add(
                "Default",
                new PreserveQueryStringRoute("{controller}/{action}/{id}", new MvcRouteHandler())
                {
                    Defaults = new RouteValueDictionary(
                        new { controller = "Home", action = "Index", id = UrlParameter.Optional })
                });   
    

    Edit:

    Ok, here’s an update.. its a bug fix, and an example of how to register the route (See corrected code above for bug fix)

    Here’s how to register it:

        routes.Add(      
        "Search",      
        new SearchRoute("Search", new MvcRouteHandler())      
        {      
            Constraints = new RouteValueDictionary(      
                new { controller = "Search", action = "Index" })      
        }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a partial view that returns an HTML chunk of list items that
I have an HTML <TABLE> displaying a list of items in the rows of
I have this in my partial view: <tr> <% for (int currentDay = 0;
in a partial view I have the following: <%Html.RenderAction(MVC.User.GetComments(Model.UserGroupName)); %> can I render a
I have a partial view that loops through its Model (a list of things)
I have a partial view which has dual list boxes and four buttons; exactly
I have ascx partial view with html-layout like that <%=Html.ActionLink<PersonController>(x => x.Publications(param1, param2, ...
Suppose that I have this partial view: Your name is <strong>@firstName @lastName</strong> which is
I am displaying a partial view (shopping cart) inside my main (pricing view) view
I have a Partial View with two opposite ActionLinks inside: <div id=divStatus> @if (IsAuthenticated)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.