3

This is effectively a duplicate of How Can I Stop ASP.Net MVC Html.ActionLink From Using Existing Route Values?

According to @Eilon -

Because once you invalidate a parameter segment then all parameter segments after it will get invalidated.

In my case, I have two Action links that link to actions on separate controllers.

Both actions have an optional page parameter.

The route configurations look like:

    // blog routes

    routes.MapRoute(
        "",
        "blog/page{page}",
        new { controller = "Blog", action = "Index" },
        new { page = @"\d+" }
    );

    routes.MapRoute(
        "",
        "blog/{id}",
        new { controller = "Blog", action = "Post" }
    );

    routes.MapRoute(
        "",
        "blog",
        new { controller = "Blog", action = "Index" }
    );

    // project routes

    routes.MapRoute(
        "",
        "projects/page{page}",
        new { controller = "Project", action = "Index" },
        new { page = @"\d+" }
    );

    routes.MapRoute(
        "",
        "projects/{id}",
        new { controller = "Project", action = "Project" }
    );

    routes.MapRoute(
        "",
        "projects",
        new { controller = "Project", action = "Index" }
    );

The problem is that if I navigate to page 2 of projects, and click the blog link, I will be directed to /blog/page2.

Now according to the above, if a parameter segment is invalidated (in this case both the controller and action are different) then the page parameter should be invalidated. Does this work if the controller and action are specified using the standard ActionLink overloads, or does it only apply if they are passed in via the RouteValueDictionary?

Currently I have had to create a custom ActionLink to remove the "page" parameter from RouteData.

Thanks, Ben

Community
  • 1
  • 1
Ben Foster
  • 34,340
  • 40
  • 176
  • 285
  • possible duplicate of [How Can I Stop ASP.Net MVC Html.ActionLink From Using Existing Route Values?](http://stackoverflow.com/questions/1817421/how-can-i-stop-asp-net-mvc-html-actionlink-from-using-existing-route-values) – George Stocker Jan 16 '11 at 15:19
  • If it's a duplicate, then why are you asking it again? – George Stocker Jan 16 '11 at 15:19
  • @George Stocker - I am asking it again because the solution provided on that post is not working for me. – Ben Foster Jan 16 '11 at 16:35

2 Answers2

1

This maybe? How to prevent Url.RouteUrl(...) from inheriting route values from the current request

Community
  • 1
  • 1
Hawxby
  • 2,746
  • 21
  • 29
0

Probably not the ideal. But you could: Html.ActionLink("action", "controller", new { page = "" })

Seems a bit better than creating a custom helper at least. HTH

nick
  • 1,477
  • 2
  • 20
  • 29
  • @nick - unfortunately this does not appear to work. I've tried passing both page = "" and page = string(null) and the route value is still used. – Ben Foster Jan 16 '11 at 16:36
  • Try changing it to another number altogether - let's try working through in a step-by-step process. First, try and confirm you can exert control over it. Then we'll take it from there. Just to try and rule out something else is affecting it. – nick Jan 16 '11 at 16:49
  • Also, you might want to specify it as UrlParameter.Optional - maybe MVC looks into the old route values if it cannot find defaults. Unfortunately, my MVC book is on my desk at work. – nick Jan 16 '11 at 16:50
  • @nick - I've tried this. Explicitly specifying a number works fine but I don't like as it means I can never just go to /blogs - I have to go to /blogs/page2. UrlParameter.Optional doesn't work as I have a regex route constraint, in which case the route is bypassed completely. – Ben Foster Jan 16 '11 at 17:30
  • 1
    set up a route for just /blogs - totally ignoring the word page. – nick Jan 16 '11 at 17:36
  • or what about the overload where you can pass in a route values dictionary - try new RouteValueDictionary() - but leave it empty. Once again, I don't know if this will work. – nick Jan 16 '11 at 17:58
  • @nick - thanks for persisting :) Unfortunately I tried most of these suggestions prior to posting. I've included my full route configuration. Creating a new RouteValueDictionary had no effect either. It seems the only way is to either specify 1 as the page number or explicitly remove the route value like I am currently. Setting the page parameter to "" or string(null) does not work due to the fallback url - I would end up with /blog?page=2. Of course it would be much easier if I didn't want the word "page" in the url - but I do :) – Ben Foster Jan 17 '11 at 00:12