【问题标题】:T4MVC and duplicate controller names in different areasT4MVC 和不同区域的重复控制器名称
【发布时间】:2010-05-10 11:08:10
【问题描述】:

在我的应用程序中,我在默认区域(在应用程序根目录中)和我的区域名为 Manage 都有一个名为 Snippets 的控制器。我使用 T4MVC 和自定义路由,如下所示:

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

我得到这个错误:

找到了与名为“sn-ps”的控制器匹配的多种类型。如果为该请求提供服务的路由 ('{controller}/{action}/{id}/') 未指定命名空间来搜索与请求匹配的控制器,则可能会发生这种情况。如果是这种情况,请通过调用采用“namespaces”参数的“MapRoute”方法的重载来注册此路由。

“sn-ps”的请求找到了以下匹配的控制器: Snippets.Controllers.SnippetsController Snippets.Areas.Manage.Controllers.SnippetsController

我知道MapRoute 有采用namespaces 参数的重载,但是在T4MVC 支持下没有这样的重载。可能是我错过了什么?可能的语法可以是:

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

或者,将命名空间作为 T4MVC 属性对我来说似乎很好:

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

提前致谢!

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 t4mvc


    【解决方案1】:

    有道理。我猜你只是第一个遇到这种情况的人。尝试将 T4MVC.tt 中的所有 MapRoute 方法替换为以下内容:

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

    请注意,您可以在没有 T4MVC 帮助的情况下在控制器命名空间上获得强类型,只需编写:

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

    我应该补充一点,理想情况下,您根本不必传递命名空间,因为您针对特定控制器的意图已经在 MVC.Snippets.Rss() 调用中捕获。但是,如果不对 T4MVC 进行重大更改,我无法找到一种明显的方法来完成这项工作。

    无论如何,请查看并测试更改,并告诉我它对您的效果。如果看起来不错,我会弄进去的。

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      相关资源
      最近更新 更多