Meine Frage ist sehr ähnlich, andere Fragen. Wenn ich einen ActionLink in MVC .Net 4.5 verwende, erhalte ich eine Abfragezeichenfolge für einen Parameter statt nur für einen URL-Pfad. Ich habe versucht, die Lösung HERE, aber es hat nicht funktioniert.ActionLink zeigt Abfragezeichenfolge anstelle von URL-Parametern
CODE-
Innen RouteConfig.cs -
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "MyControllerRoute",
url: "{controller}/{action}/{id}/{description}",
defaults: new { controller = "MyController", action = "MyAction", id = UrlParameter.Optional, description = UrlParameter.Optional }
);
Innen HomeController.cs -
public ActionResult Index(){
--do stuff--
return View();
}
Innen MyController.cs -
public ActionResult Vote(int id, string description){
--do stuff--
return View();
}
Innen Index.cshtml
@Html.ActionLink(
"This is stuff",
"MyAction",
"MyController",
new { id = 123, description = "This-is-stuff" },
null)
Dieses Ergebnis LERNEN - (NICHT, WAS ICH WILL)
<a href="/MyController/MyAction/123?description=This-is-stuff">This is stuff</a>
Gewünschtes Ergebnis - (WIE KANN ICH DIESES?)
<a href="/MyController/MyAction/123/This-is-stuff">This is stuff</a>
Move 'MyControllerRoute' vor der Standardroute. Und entfernen Sie 'id = UrlParameter.Optional, description = UrlParameter.Optional' von der' MyControllerRoute' Route (nur der letzte Parameter kann als optional markiert werden –
Das hat funktioniert, danke Stephen! Wollen Sie eine "offizielle" Antwort machen, damit ich Kann ich Ihnen eine offizielle Antwort geben? –
Der Grund, warum ich noch keine Antwort hinzugefügt habe, ist, dass es unklar ist, warum Sie beide Parameter in der 'MyControllerRoute' Route als optional markieren es gab immer einen Fall, in dem Sie nur entweder 'id' oder 'description' angeben würden (in diesem Fall würden Sie eine andere Lösung benötigen) –