I have problem with optional parameter for one of the routes. Route looks like that:
routes.MapRoute(
"VideoList", // Route name
"VideoList/{page}", // URL with parameters
new { controller = "Video", action = "VideoList" , page = UrlParameter.Optional}
);
It works fine when used first time:
@Html.RouteLink("Video", "VideoList", null, new { id = "idVideo", @class = "menu-item" })
Controller action gets NULL in place of page parameter and I handle rest within the action.
Then, I play with the web page and I try this same routing again, this time specifying ‘page’ param, lets say it carries value 7.
@Html.RouteLink("Video", "VideoList", new { page = 7 }, new { id = "idVideo", @class = "menu-item" })
It works as expected as well, but when again I try to use route without ‘page’ specified it carries because of some reason again value used for this parameter last time, which is 7
Not sure why. I would expect it to work as first time by passing null to the controller for this parameter
I will provide more info if needed.
That’s how routing works. You need to explicitly set the parameter if you don’t want the RouteLink fetching it from the current request route values:
This will always generate
/VideoListurl. If you don’t explicitly set thepageparameter to an empty string (as in your first example), then the helper will analyze the current request routing values when generating the link. And if the current url is/VideoList/7, then the helper will preserve all route values and generate/VideoList/7.