Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a link in a page that goes like this:

Url.Action("List", "Item", new { category = "seasons" })

The Route that matches that page also has a parentGroup and group parameters

Ex: /Seasons/Moober/Blue/1 -> /{category}/{parentGroup}/{group}/{id}

The problem is when I am on that page and use Url.Action, it adds all the missing route values even when I try to generate a link to the categories only /{category}, it will still add parentGroup and group.

I have found this post, that suggests doing it like this:

Url.Action("List", "Item", new { category = "seasons", group = "", parentGroup = "" })

But it does not work for me as it removes them from my url but adds them as parameters: /Seasons?parentGroup=Moober&group=Blue

I am using MVC3. Is there a way to force Url.Action() to use only the provided parameters or to cancel the ones I do not want?

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 for the moment that looks like this: Url.RouteUrl("ItemListFromCategory") I am basically forcing the same route that is supposed to be matched by Url.Action("List", "Item", new { category = "seasons" }) And this time no parameters added automatically.

The problem with this is that I am forced to use named routes.

like image 295
Emanuel Avatar asked Aug 08 '11 06:08

Emanuel


1 Answers

Change your routes

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

Make parentGroup , parentGroup, group optional.

like image 67
Praveen Prasad Avatar answered Oct 26 '22 23:10

Praveen Prasad