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

  • Home
  • SEARCH
  • 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 679471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:15:48+00:00 2026-05-14T01:15:48+00:00

I’m implementing a site which has a search that can be executed via Controller

  • 0

I’m implementing a site which has a search that can be executed via Controller methods that are accessed from two different routes. One hangs off the default route (using either Post data or query string values for the search parameters) and one is an SEO optimisation url which takes a location and phrase via two route values. The second route looks like this:

routes.MapRoute("SEOSearch", "Search/{seoLocation}/{seoSearchString}",
  new { controller = "Search", 
        action = "SEOResults", 
        seoLocation = (string)null, 
        seoSearchString = (string)null });

You might be wondering why I have two different routes – it’s because the search offers many other parameters than just location and phrase – but I want the SEO’d urls to include those two in the path, rather than using the query string.

As I say, the first route is the default /controller/action/id route, and the correct controller/action for that is “Search” and “Index”.

In the end, both actions execute the same search operation in the controller, and both will render their results using the Index view, since their result models are identical.

On the index view I use a partial view for the search terms, another partial for the results and another for the paging.

The problem I’m having is getting the paging control to render the correct link to launch the current search for the next page using the same URL format as the current request.

What I want

So, assuming you’ve navigated to /Search?Location=[location]&Phrase=[phrase], I want Page 2’s link generated by the pager to be /Search?Location=[location]&Phrase=[phrase]&Page=2.

However, if you’ve launched the search with /Search/[location]/[phrase], I want Page 2’s link to be /Search/[location]/[phrase]?Page=2.

What I’ve got

The closest I’ve got is this:

<%=  Html.RouteLink("Previous Page", 
       RouteHelpers.Combine(ViewContext.RouteData.Values, 
       new RouteValueDictionary() { { "Page", Model.Results.PageNo + 1}})) %>

Where RouteHelpers.Combine is an extension that I’ve written that takes two objects and merges them into one RouteValueDictionary. By taking the RouteValues for the current request, I’m able to persist the current Controller and Action name (without having to know what they are) – however this misses some important information from ModelState – i.e. any extra search parameters that were provided – i.e. it works if the current Url is /Search/London/Widgets, but if it’s /Search/London/Widgets?PageSize=50 then the PageSize parameter doesn’t get persisted into the outgoing link.

Even worse, if it’s a non-SEO’d url – i.e. /Search?Location=London&Phrase=Widgets, the outgoing url simply becomes /Search?Page=x.

My search parameters are read from the request into a model type, that is then fed to both the host page, and to the pager itself, so in theory I could simply always generate them from that – but I end up with all the parameters in the url, even when they are default values (therefore they do not need to be supplied) – so the url looks ugly.

How do I achieve what I want!? I’m feeling like I know nothing about MVC all of sudden!

  • 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-14T01:15:48+00:00Added an answer on May 14, 2026 at 1:15 am

    You don’t have to use your RouteHelper to combine route values. Those values are combined with your anonymous object automatically when you use ActionLink instead. All existing values will be overwritten and new ones added. This call does that for you:

    RouteValueDictionary values = RouteValuesHelpers.MergeRouteValues(
        actionName,
        controllerName,
        requestContext.RouteData.Values,
        routeValues,
        includeImplicitMvcValues); // true for ActionLink; false for RouteLink
    

    Where routeValues are your values from anonymous object. They get merged to requestContext.RouteData.Values.

    So you can still use:

    Html.ActionLink(
        "whatever",
        this.ViewContext.RouteData.Values["action"],
        this.ViewContext.RouteData.Values["controller"],
        new { Page = /* whatever needs to be */ },
        null)
    

    Null at the end is mandatory, so it doesn’t confuse anonymous object with HTML attributes.

    But you do have routing problems unless you also put a route constraint on your default route to only use certain actions, otherwise your /Search/SeoLocation/SeoSearchString will still be handled by your default route, by SeoLocation becoming your action and SeoSearchString the id.

    Anyway. If your code would be correct all those values should be inside your route values dictionary and propagated to your links as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.