4

Here's my route setup:

routes.MapRoute(
    name: "MyRoute",
    url: "Secret/{source}/{display}/{sort}/{tags}/{filter}/{pageSize}/{page}",
    defaults: new { controller = "Secret", action = "Index", page = 1, filter = "-", tags = "-" },
    namespaces: new[] { "SomeProject.Controllers" }
    );

When I access this URL:

http://localhost:12345/Secret/SourceHere/Gallery/Score/-/-/26

and my view has this markup:

<a href="@Url.Action("Index", new { tags = @tagInfo.Tag })">

then the resulting URL in the link is:

http://localhost:12345/Secret?tags=hello

Though I can't find official documentation for it, everything I read on SO, Google etc says that route values should be preserved. Well, I've worked around it by specifying all route values every time, but that sucks.

Why aren't my route values being preserved?

EDIT my answer below is not quite right. In another view/situation, the rules there do not apply. Can anyone explain in absolute certainty the rules for when route values are remembered or not?

Routes drive me effin' crazy.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
  • Discussion [moved to chat.](http://chat.stackoverflow.com/rooms/20417/discussion-between-robert-harvey-and-kieren-johnstone) – Robert Harvey Nov 30 '12 at 23:27

1 Answers1

3

In turns out (thanks Robert Harvey) that making sure all route parameters have default values (of anything at all) meant the view would generate links remembering current route values.

I would like to point out that this is really confusing: the default values aren't used for anything, but they need to be specified so values are remembered!

The only thing is, it does only remember up to the first parameter whose value is being specified. Any parameters after that are lost (even those with a non-default value).

So the answer at present is: specify some (any!) default values for all route parameters, and remember to specify new values for the parameters you want, plus any coming after them in the route URL.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144