2016-10-24 5 views
-1

Ich habe ein Problem mit Html.ActionLink Methode.Html.ActionLink mit mehr als einem Routenwert

Wenn ich

@Html.ActionLink("Some text", "MyAction", "MyController", new { id = 1234 }, null) 

ich einen Link mit diesem href erhalten:

http://web.com/MyController/MyAction/1234 

Das ist in Ordnung, aber wenn ich weitere Routenwerte verwenden wie

@Html.ActionLink("Some text", "MyAction", "MyController", new { id = 1234, param1 = 3, param2 = 10 }, null) 

ich eine Verbindung mit dieser href:

http://web.com/MyController/MyAction/1234?param1=3&param2=10 

Aber ich brauche:

http://web.com/MyController/MyAction/1234/3/10 

Wissen Sie, wie kann ich es bekommen?

Ausgabe weitere Informationen zu geben:

In MyController Code-Datei Ich habe das:

[Route("MyController/MyAction/{id}")] 
public ActionResult MyAction(string id) { /* some code */ } 

[Route("MyController/MyAction/{id}/{param1}/{param2}")] 
public ActionResult MyAction(string id, byte param1, byte param2) { /* some code */ } 

Und das ist meine RouteCofig.cs Datei:

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

     routes.MapMvcAttributeRoutes(); 

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

Ich habe dieses Attribut '[Route (" MyController/MyAction/{id}/{param1}/{param2} "]] in der Aktion' public ActionResult MyAction (Zeichenfolgen-ID, Byte param1, Byte param2) ', I Ich dachte, es gäbe dasselbe. – Jon

+0

können Sie uns Ihre Route Config zeigen? –

+0

Frage bearbeitet mit Controller und Route Config. Fehle ich etwas? – Jon

Antwort

1

Hier ist Ihre Lösung,

routes.MapRoute(
     name: "MyRoute",           // Route name 
     url: "{controller}/{action}/{id}/{param1}/{param2}",       // URL with parameters 
     defaults: new { controller = "MyController", action = "MyAction", id = "", param1="", param2="" } // Parameter defaults 
); 
Verwandte Themen