MVC4 de routage @Html.ActionLink et @Html.RouteLink la production de mal url

Salut les gars, peut-être quelqu'un peut m'aider à comprendre pourquoi @Html.ActionLink et @Html.RouteLink produisant les liens mauvais dans certains cas.

J'ai routes déclarées comme ça:

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

            routes.MapRoute(
                "Catalog", //Route name
                "Catalog/{a}/{b}/{c}/{d}/{e}", //URL with parameters
                new { controller = "Catalog", action = "Index", a = UrlParameter.Optional, b = UrlParameter.Optional, c = UrlParameter.Optional, d = UrlParameter.Optional, e = UrlParameter.Optional } //Parameter defaults
            );

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

J'ai contrôleur avec mes params:

public ActionResult Index(string a, string b, string c, string d, string e)
        {
        //Do stuff;
        }

J'ai la liste de cas avec @Html.RouteLink et @Html.ActionLink sur la vue déclarée comme ça:

    @Html.ActionLink("ActionLink1","Index", new { a = "a" }, null)
@Html.ActionLink("ActionLink2","Index", new { a = "a", b = "b" }, null)
@Html.ActionLink("ActionLink3","Index", new { a = "a", b = "b", c = "c" }, null)
@Html.ActionLink("ActionLink4","Index", new { a = "a", b = "b", c = "c", d = "d" }, null)
@Html.ActionLink("ActionLink5","Index", new { a = "a", b = "b", c = "c", d = "d", e = "e" }, null)
<br/>
@Html.RouteLink("RouteLink1","Catalog", new { a = "a" })
@Html.RouteLink("RouteLink2","Catalog", new { a = "a", b = "b" })
@Html.RouteLink("RouteLink3","Catalog", new { a = "a", b = "b", c = "c" })
@Html.RouteLink("RouteLink4","Catalog", new { a = "a", b = "b", c = "c", d = "d" })
@Html.RouteLink("RouteLink5","Catalog", new { a = "a", b = "b", c = "c", d = "d", e = "e" 
})

Les Résultats

Url http://localhost:2288/Catalog

ActionLink1

  • Résultat Réel: http://localhost:2288/Catalog?a=a
  • Résultat Attendu: http://localhost:2288/Catalog/a

RouteLink1

  • Résultat Réel: http://localhost:2288/Catalog
  • Résultats Attendus: http://localhost:2288/Catalog/a

Url http://localhost:2288/Catalog/a

ActionLink1

  • Résultat Réel: http://localhost:2288/Catalog?a=a
  • Résultats Attendus: http://localhost:2288/Catalog/a

Url http://localhost:2288/Catalog/a/b

ActionLink1

  • Résultat Réel: http://localhost:2288/Catalog/a/b
  • Résultats Attendus:http://localhost:2288/Catalog/a

RouteLink1

  • Résultat Réel: http://localhost:2288/Catalog/a/b
  • Résultats Attendus: http://localhost:2288/Catalog/a

Url http://localhost:2288/Catalog/a/b/c

ActionLink1

  • Résultat Réel: http://localhost:2288/Catalog/a/b/c
  • Résultats Attendus: http://localhost:2288/Catalog/a

RouteLink1

  • Résultat Réel: http://localhost:2288/Catalog/a/b/c
  • Résultats Attendus: http://localhost:2288/Catalog/a

ActionLink2

  • Les Résultats Réels: http://localhost:2288/Catalog/a/b/c
  • Résultats Attendus: http://localhost:2288/Catalog/a/b

RouteLink2

  • Les Résultats Réels: http://localhost:2288/Catalog/a/b/c
  • Résultats Attendus: http://localhost:2288/Catalog/a/b

Et ainsi de suite...


Url http://localhost:2288/Catalog/a/b/c/d

ActionLink1, ActionLink2 et ActionLink3 ainsi que RouteLink1, RouteLink2, et RouteLink3 sont tous les producteurs de mauvais liens.


Url http://localhost:2288/Catalog/a/b/c/d/e

Tous ActionLinks et RouteLinks (exepté ActionLink5 et RouteLinks5) sont la production de mauvais liens.


J'ai mis un exemple de projet ici: http://www.mediafire.com/?gdbatoafgd0kf4w

Peut-être quelqu'un peut comprendre pourquoi ce qui se passe?

Que l'histoire a commencé il y a quelques jours quand j'ai essayé de construire la Chapelure avec MvcSiteMapProvider, et j'ai même des liens produite par MvcSiteMapProvider Breadcrumbs.
Lors de mes recherches, je me figure que MvcSiteMapProvider pas à l'origine du problème le problème ailleurs. J'ai donc créé par défaut MVC4 projet et de son étrange comportement par défaut.

Mise à JOUR

Il ressemble quand vous utilisez @Html.ActionLink et @Html.RouteLink aides url généré sur la base de l'url actuelle..mais ne peut toujours pas comprendre pourquoi quand Current url http://localhost:2288/Catalog j'obtiens:

http://localhost:2288/Catalog?a=a au lieu de http://localhost:2288/Catalog/a en cas de ActionLink

et http://localhost:2288/Catalog au lieu de http://localhost:2288/Catalog/a en cas de RouteLink

pense que ce post va répondre à votre question haacked.com/archive/2011/02/20/...
aussi route existante valeurs sont utilisées lors de la génération d'url. Provoquant ActionLink1 = 'localhost:2288/Catalogue/a/b/c"; dans CurrentUrl = 'localhost:2288/Catalogue/a/b/c';. Malheureusement, vous devez définir explicitement ces valeurs de chaîne.Vide dans le actionlink.

OriginalL'auteur Joper | 2013-04-19