2010-05-08 9 views
5

In meiner Anwendung habe ich Controller namens Snippets sowohl im Standardbereich (in Anwendungsstamm) und in meinem Bereich Manage genannt. Ich benutze T4MVC und individuelle Routen, wie folgt aus:T4MVC und doppelte Controller-Namen in verschiedenen Bereichen

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

Und ich bekomme diese Fehlermeldung:

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

Ich weiß, dass es Überlastungen für MapRoute die namespaces Argument, aber es gibt keine solche Überlastungen mit T4MVC Unterstützung. Kann mir etwas fehlen? Die mögliche Syntax kann sein:

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

oder, wie es mir recht gut scheint Namespace als T4MVC Eigenschaft zu haben:

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

Vielen Dank im Voraus!

Antwort

5

Sinn macht. Ich schätze, du bist nur der erste, der darauf reinkommt. Versuchen Sie, alle MapRoute Methoden in T4MVC.tt ersetzt durch:

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

Beachten Sie, dass die starke Typisierung auf dem Controller-Namespace ohne T4MVC Hilfe einfach durch Schreiben bekommen kann:

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

Ich sollte hinzufügen, dass Im Idealfall müssten Sie die Namespaces überhaupt nicht übergeben, da Ihre Absicht, einen bestimmten Controller zu steuern, bereits im MVC.Snippets.Rss() - Aufruf erfasst wurde. Allerdings konnte ich keinen offensichtlichen Weg finden, dies ohne große Änderungen an T4MVC zu machen.

Wie auch immer, bitte überprüfen und testen Sie die Änderung, und lassen Sie mich wissen, wie es für Sie funktioniert. Wenn es gut aussieht, werde ich es bekommen.

Danke!