2010-05-08 9 views
5

Trong ứng dụng của tôi, tôi có bộ điều khiển tên là Snippets cả trong khu vực mặc định (trong ứng dụng gốc) và trong khu vực của tôi được gọi là Manage. Tôi sử dụng T4MVC và tùy chỉnh tuyến đường, như thế này:T4MVC và tên bộ điều khiển trùng lặp trong các khu vực khác nhau

routes.MapRoute(
    "Feed", 
    "feed/", 
    MVC.Snippets.Rss() 
); 

Và tôi nhận được lỗi này:

Multiple types were found that match the controller named 'snippets'. This can happen if the route that services this request ('{controller}/{action}/{id}/') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'snippets' has found the following matching controllers: Snippets.Controllers.SnippetsController Snippets.Areas.Manage.Controllers.SnippetsController

Tôi biết rằng có quá tải cho MapRoute mà phải mất namespaces cãi nhau, nhưng không có quá tải như vậy với Hỗ trợ T4MVC. Có thể tôi đang thiếu một cái gì đó? Cú pháp có thể có thể là:

routes.MapRoute(
    "Feed", 
    "feed/", 
    MVC.Snippets.Rss(), 
    new string[] {"Snippets.Controllers"}   
); 

hay, có vẻ như khá tốt với tôi để có không gian tên là tài sản T4MVC:

routes.MapRoute(
    "Feed", 
    "feed/", 
    MVC.Snippets.Rss(), 
    new string[] {MVC.Snippets.Namespace}   
); 

Cảm ơn trước!

Trả lời

5

Làm cho tinh thần. Tôi đoán bạn chỉ là người đầu tiên chạy vào đây. Hãy thử thay thế tất cả các phương pháp MapRoute trong T4MVC.tt bởi những điều sau:

public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result) { 
     return MapRoute(routes, name, url, result, null /*namespaces*/); 
    } 

    public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, object defaults) { 
     return MapRoute(routes, name, url, result, defaults, null /*constraints*/, null /*namespaces*/); 
    } 

    public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, string[] namespaces) { 
     return MapRoute(routes, name, url, result, null /*defaults*/, namespaces); 
    } 

    public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, object defaults, object constraints) { 
     return MapRoute(routes, name, url, result, defaults, constraints, null /*namespaces*/); 
    } 

    public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, object defaults, string[] namespaces) { 
     return MapRoute(routes, name, url, result, defaults, null /*constraints*/, namespaces); 
    } 

    public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, object defaults, object constraints, string[] namespaces) { 
     // Start by adding the default values from the anonymous object (if any) 
     var routeValues = new RouteValueDictionary(defaults); 

     // Then add the Controller/Action names and the parameters from the call 
     foreach (var pair in result.GetRouteValueDictionary()) { 
      routeValues.Add(pair.Key, pair.Value); 
     } 

     var routeConstraints = new RouteValueDictionary(constraints); 

     // Create and add the route 
     var route = new Route(url, routeValues, routeConstraints, new MvcRouteHandler()); 

     if (namespaces != null && namespaces.Length > 0) { 
      route.DataTokens = new RouteValueDictionary(); 
      route.DataTokens["Namespaces"] = namespaces; 
     } 

     routes.Add(name, route); 
     return route; 
    } 

Lưu ý rằng bạn có thể nhận gõ mạnh vào không gian tên điều khiển mà không cần sự giúp đỡ T4MVC chỉ đơn giản bằng cách viết:

string[] { typeof(MyApplication.Controllers.SnippetsController).Namespace } 

tôi nên thêm rằng lý tưởng, bạn sẽ không phải vượt qua Namespaces chút nào, vì mục đích của bạn để nhắm tới một bộ điều khiển cụ thể đã được ghi lại trong cuộc gọi MVC.Snippets.Rss(). Tuy nhiên, tôi không thể tìm thấy một cách rõ ràng để thực hiện công việc này mà không có những thay đổi lớn đối với T4MVC.

Dù sao, vui lòng xem lại và kiểm tra thay đổi và cho tôi biết cách thức hoạt động của bạn thay đổi. Nếu có vẻ tốt, tôi sẽ mua nó.

Cảm ơn!