5

Ich habe eine dynamische Liste von ActionLinks zur Laufzeit wie folgt generiert:MVC Routing von/Index in URL loszuwerden

@Html.ActionLink(x.Name, "Index", "User", new { x.ID }, null) 

so die Index-Methode auf den User-Controller Ich schlage.

Ich habe auch meine RouteConfig.cs (es ist ein MVC4 app) wie folgt festgelegt:

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

     routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 

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

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

Das gibt mir den Link server/application/User/Index/1 in meiner Liste von Links.

Was muss ich in der Listengenerierung und/oder Routing-Datei tun, um dies in server/application/User/1 zu ändern?

Antwort

9

eine Route hinzufügen, die eine Aktion nicht brauchen, aber mit anderen Routen

routes.MapRoute("User", "User/{id}", 
         new { controller = "User", action = "Index" }); 
+0

Arbeitete mochte einen Reiz nicht aufeinanderprallen - danke! – markp3rry