0

I'm using ASP.Net 4.5 w/ MVC 5.2.2.0, and trying to add breadcrumb style navigation to an app that has five levels of navigational depth. I got to the fourth level, and noticed a weird problem during testing a proof of concept page. All but the lowest level work fine.

Here is the routing rule:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

The model looks like this:

public class TestModel : BaseModel
{
    public string id { get; set; }
    public string id2 { get; set; }
    public string id3 { get; set; }
    public int? id4 { get; set; }
    public int? id5 { get; set; }

    ...
}

Here is the code for my view:

Action
&gt; <a href="@Url.Action("Action", new { id = Model.id, id2 = null as object, id3 = null as object, id4 = null as object })">@Model.IdName</a>
&gt; <a href="@Url.Action("Action", new { id = Model.id, id2 = Model.id2, id3 = null as object, id4 = null as object })">@Model.Id2Name</a>
&gt; <a href="@Url.Action("Action", new { id = Model.id, id2 = Model.id2, id3 = Model.id3, id4 = null as object })">@Model.Id3Name</a>
&gt; <a href="@Url.Action("Action", new { id = Model.id, id2 = Model.id2, id3 = Model.id3, id4 = Model.id4 })">@Model.Id4Name</a>

This code renders the following HTML for /Controller/Action/X/Y/Z/4:

Action
&gt; <a>X</a>
&gt; <a href="/Controller/Action/X/Y">Y-Name</a>
&gt; <a href="/Controller/Action/X/Y/Z">Z-Name</a>
&gt; <a href="/Controller/Action/X/Y/Z/4">4-Name</a>

Why does the first level, /Controller/Action/X, not generate like the others?

Jake Braun
  • 1,172
  • 1
  • 13
  • 34
  • I don't know for sure, but I would guess that Url.Action is viewing those routes as some kind of cross-area route. Whatever the case, ASP.NET and Razor are open source. Go take a look for yourself. – Sam Axe Mar 09 '15 at 00:56
  • 1
    Please post your *other* routing rules in the order which they appear in your configuration. Your action is probably matching a different route, which is why it is not generating the URL. – NightOwl888 Mar 09 '15 at 05:14
  • It's the only rule, but I've added the entire RouteConfig class to the question for clarity. – Jake Braun Mar 09 '15 at 12:12

0 Answers0