I defined two action links in my view like below:
@Html.ActionLink("Baxter", "SearchAffaires", new { LabelName = "Baxter" })
@Html.ActionLink("Favorite", "SearchAffaires", new { OnlyFavorite = true })
I defined the two routes below:
routes.MapRoute(
"Search Affaire Only Label", // Route name
"{controller}/Label/{LabelName}", // URL with parameters
new { controller = "Affaire", action = "SearchAffaires" } // Parameter defaults
);
routes.MapRoute(
"Search Affaire Only Favorite", // Route name
"{controller}/Favorite", // URL with parameters
new { controller = "Affaire", action = "SearchAffaires", OnlyFavorite = true } // Parameter defaults
);
If I start my solution and point to my ‘favorite’ link, I get the following url:
http://localhost:3817/Affaire/Favorite
That’s ok for me. Now, I restart my solution (important) and I point to a ‘label’ link (for this example, my label is Baxter), I get the following url:
http://localhost:3817/Affaire/Label/Baxter
That’s ok for me. Now I didn’t restart my solution and I point to my ‘favorite’ link, I get the following url:
http://localhost:3817/Affaire/Label/Baxter?OnlyFavorite=True
As you can see, the parameter named OnlyFavorite is passed again but I didn’t expect that!
What can I do to avoid this behavour?
Thanks.
The problem is at the time you add the link MVC has no way of knowing which route you want to use, as it has all the parameters from the current request,
LabelNameis set, so it matches on theSearch Affaire Only Labelroute.Here’s a fix. likewise you could alternatively add a constraint that
LabelNamecan’t be empty, so it matches on the second route: