T4MVC和不同区域的重复控制器名称

时间:2010-05-08 13:06:22

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

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

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

我收到了这个错误:

  

发现多个类型与名为“snippets”的控制器匹配。如果为此请求提供服务的路由('{controller} / {action} / {id} /')未指定名称空间来搜索与请求匹配的控制器,则会发生这种情况。如果是这种情况,请通过调用带有'namespaces'参数的'MapRoute'方法的重载来注册此路由。

     

'snippets'的请求找到了以下匹配的控制器:   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}           
);

提前致谢!

1 个答案:

答案 0 :(得分:5)

有道理。我想你只是第一个遇到这种情况的人。尝试通过以下方法替换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进行大的改动,我找不到明显的方法来完成这项工作。

无论如何,请检查并测试更改,并告诉我它是如何工作的。如果看起来不错,我会把它拿进来。

谢谢!

相关问题