I have a link on a page that looks something like this:
Url.Action("List", "Item", new { category = "seasons" })
The route corresponding to this page also has parent group and group settings.
Example: / Seasons / Moober / Blue / 1 â / {category} / {parentGroup} / {group} / {id}
The problem is that when I am on this page and using Url.Action, it adds all the missing route values, even when I try to create a link to only the / {category} category, it will still add parentGroup and group.
I found this post that suggests doing this as follows:
Url.Action("List", "Item", new { category = "seasons", group = "", parentGroup = "" })
But this does not work for me, as it removes them from my url, but adds them as parameters: / Seasons parentGroup = Moober &? Band = Blue
I am using MVC3. Is there any way to force Url.Action () to use only the provided parameters or cancel those that I don't need?
Here are the routes.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "ItemFromCategoryParentGroupSubGroup", // Route name "{category}/{parentGroup}/{group}/{id}/{language}", // URL with parameters new { controller = "Item", action = "Show", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults new { category = _validCategory, parentGroup = _validGroup, group = _validChildGroup, id = _validItemInChildGroup } ); routes.MapRoute( "ItemListFromParentGroup", // Route name "{category}/{parentGroup}/{group}/{language}", // URL with parameters new { controller = "Item", action = "List", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults new { category = _validCategory, parentGroup = _validGroup, group = _validChildGroup } ); routes.MapRoute( "ItemWithGroup", // Route name "{category}/{group}/{id}/{language}", // URL with parameters new { controller = "Item", action = "Show", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults new { category = _validCategory, group = _validGroup, id = _validItemInGroup } ); routes.MapRoute( "ItemListWithGroup", // Route name "{category}/{group}/{language}", // URL with parameters new { controller = "Item", action = "List", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults new { category = _validCategory, group = _validGroup } ); routes.MapRoute( "ItemListFromCategory", // Route name "{category}/{language}", // URL with parameters new { controller = "Item", action = "List", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults new { category = _validCategory } );
Edit:
I have a workaround at that point that looks like this: Url.RouteUrl ("ItemListFromCategory") I basically force the same route, which is supposed to match Url.Action ("List", "Item", new { category = "seasons"}) And this time, no parameters are added automatically.
The problem is that I am forced to use named routes.