Url.Action () includes unwanted parameters, trying to get more control

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.

+7
source share
3 answers

In the end, I create custom constants for each route.

http://msdn.microsoft.com/en-us/library/system.web.routing.route.constraints.aspx

I found that it gives almost complete control over what we want to do with our routes.

0
source

Change routes

 routes.MapRoute( "SomeRoute", // Route name "/{category}/{parentGroup}/{group}/{id}", new { controller = "DefaultController", action = "DefaultAction", parentGroup = UrlParameter.Optional group = UrlParameter.Optional id= UrlParameter.Optional } ); 

Create parentGroup, parentGroup, optional group.

+3
source

Try declaring the other parameters as UrlParameter.Optional when registering the route and setting a default value for these parameters in your action. For example.

 MyAction(string category, string group = "", parentGroup = 0) 

Hope this helps.

0
source

All Articles