MVC4 - route values โ€‹โ€‹are not remembered

Here is 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 opinion has this markup:

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

then the resulting URL in the link:

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

Although I canโ€™t find the official documentation for it, everything that I read on SO, Google, etc., says that the route values โ€‹โ€‹should be saved. Well, I worked around this, each time indicating all the route values, but that sucks.

Why are my route values โ€‹โ€‹not saved?

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

Routes infuriate me.

+4
source share
1 answer

As a result (thanks to Robert Harvey), making sure that all route parameters have default values โ€‹โ€‹(anything at all) means that the view will generate links that remember the current route values.

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

The only thing he remembers only up to the first parameter, the value of which is given. Any parameters after this are lost (even those with a non-default value).

Thus, the answer at the moment is: specify some (any!) Default values โ€‹โ€‹for all route parameters and do not forget to specify new values โ€‹โ€‹for the parameters you want, plus any subsequent ones in the route URL.

+3
source

All Articles