5

tôi có một danh sách năng động của ActionLinks tạo ra tại thời gian chạy như thế này:MVC Routing thoát khỏi/Index trong URL

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

vì vậy tôi đánh phương pháp Index trên bộ điều khiển tài khoản.

Tôi cũng có RouteConfig.cs của tôi (đó là một ứng dụng MVC4) thiết lập như sau:

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 } 
     ); 
    } 

này mang lại cho tôi liên kết server/application/User/Index/1 trong danh sách các liên kết.

Tôi cần làm gì trong việc tạo danh sách và/hoặc tệp định tuyến để thay đổi điều này thành server/application/User/1?

Trả lời

9

Thêm một tuyến đường mà không cần một hành động nhưng sẽ không xung đột với các tuyến đường khác

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

Làm việc thích một nét duyên dáng - cảm ơn! – markp3rry