3

Consider the following sidebar:

enter image description here

If I press the sub menu item All solutions under the menu item Solutions, I would see a grid somewhat like this one:

enter image description here

The url for this grid is https://something.com/Solutions. Now imagine that I wanted to go into the menu item Partners and sub menu item Active, the url would be https://something.com/Partners/active.

I'm using a simple Html.ActionLink like so:

Html.ActionLink("- Active", "Active", "Partners", null, new { @class = "filter-button" })

PROBLEM

Navigating the user to the url https://something.com/Partners/active using the Html.ActionLink the way I do, this should work, but it doesn't. Instead of redirecting me to the active partners view, the application redirects me back to https://something.com/Solutions (exactly where I came from) but with this url: https://something.com/Solutions?undefined=undefined.

If I press the Activesub menu item in the menu item Partners once more, the url hanges to https://something.com/Solutions?undefined=undefined&undefined=undefined and this goes on forever.

I have no idea why this occurs nor do I have any solution.

PARTNERS/ACTIVE

public ActionResult Active()
{
    using(var db = new DatabaseContext())
    {
        var partners = db.Partners.Where(x => x.Active).ToList();

        return View(partners);
    }
}

ROUTE DEFINITIONS

routes.MapRoute(
    name: "ControllerIdActionId",
    url: "{controller}/{id}/{action}/{id2}",
    defaults: new { id2 = UrlParameter.Optional }
);
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
    name: "Solutions",
    url: "{controller}/{id}/{action}/",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

UPDATE

After reviewing the route definitions, I noticed that the route definition Solutions was never used. I've now removed that route definition so the route definitions look like this:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
    name: "ControllerIdActionId",
    url: "{controller}/{id}/{action}/{id2}",
    defaults: new { id2 = UrlParameter.Optional }
);

This had made no difference though. I tried changing the two routes around so the ControllerIdActionId route would be registered before the Default, but this has no effect either.

halfer
  • 19,824
  • 17
  • 99
  • 186
Detilium
  • 2,868
  • 9
  • 30
  • 65
  • Can you post this action `/Partners/active` – Lucian Bumb Jul 25 '16 at 07:24
  • Show your route definitions as well –  Jul 25 '16 at 07:26
  • Both have been added. – Detilium Jul 25 '16 at 07:35
  • Your routes are out of order (and they do not make sense). Temporarily comment out both the `ControllerIdActionId` and `Solutions` routes and check it –  Jul 25 '16 at 07:37
  • @StephenMuecke how did you find out the "routes are out of order"? – user786 Jul 25 '16 at 07:39
  • @StephenMuecke That could prove a challenge as I'm using the route definition `Solutions` in my grid. If this is the problem, are their other ways I can test it, perhaps even a possible solution? – Detilium Jul 25 '16 at 07:39
  • @Alex,They make no sense :) –  Jul 25 '16 at 07:40
  • @StephenMuecke how to make routes to make sense, if I have multiple routes, does it matter the order in which they are coded? – user786 Jul 25 '16 at 07:41
  • @Detilium, You need to explain what you trying to achieve with those routes. They simply cannot work correctly as you have shown them. If you want the `Active()` method of `SolutionsController` to generate `.../Solutions`, then you need a specific route - `routes.MapRoute( name: "Solutions", url "Solutions", defaults: new { controller = "Solutions", action = "Active } }` and it needs to be the first one –  Jul 25 '16 at 07:42
  • @StephenMuecke I haven't really looked into the route definitions as I didn't create them in the first place. I'll take a look at them and see if I can find a solution. – Detilium Jul 25 '16 at 07:48
  • @StephenMuecke No wait, I think you misunderstood my structure, or I wrote it wrong at least. The method I'm hitting in not `Active()` but `Index()` as the sub menu item hit is called `All solutions` and not `Active`. This is a typo from my part. Will update the post. – Detilium Jul 25 '16 at 07:53
  • I think you need to do some research on routing in MVC :). I don't want to write a book chapter, but your first route means - match anything with 3 or 4 segments in it - so (say) `../Product/Edit/1` would match it because it contains 3 segments (and its the first route). As it is, the other 2 routes are a bit pointless because it will never get that far. –  Jul 25 '16 at 07:54
  • @StephenMuecke Except, with `RouteLink()`'s I'm capable of telling MVC what route definition to use. I suppose this was the main idea behind it. – Detilium Jul 25 '16 at 07:59
  • You still need to place your routes in order (most specific first) - a user can type a url in the address bar :) –  Jul 25 '16 at 08:02
  • `ControllerIdActionId` should take place on top of `Default` one. The MVC routing system is top-down processing model, most specific routes should defined first. – Tetsuya Yamamoto Jul 25 '16 at 08:23
  • See [Why map special routes first before common routes in asp.net mvc?](http://stackoverflow.com/a/35674633/181087). Both of your routes have much overlap on which URLs they match, but the first match always wins so you are sure to get unexpected behavior with this configuration. Placeholders can contain *any value*, they are given a name, but that doesn't mean routing will understand what they are for. See my link for some possible solutions. – NightOwl888 Jul 25 '16 at 08:36
  • @NightOwl888 I can see what you mean, but I'm just not sure what the solution is. – Detilium Jul 25 '16 at 09:14

1 Answers1

0

The order of the routes is important. Please change the order of the routes. Move the Default route to 1st position and try.

Also the routes ControllerIdActionId and Solutions needs to be refined, they look out of order. Please try to follow certain pattern to avoid complications.

E.g. Controller/Action/ID

Gautam B
  • 11
  • 3