5

在我的应用程序中,我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

我知道MapRoutetakenamespaces参数有重载,但是 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}           
);

提前致谢!

4

1 回答 1

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 进行重大更改,我无法找到一种明显的方法来完成这项工作。

Anyway, please review and test the change, and let me know how it works for you. If it looks good, I'll get it in.

Thanks!

于 2010-05-09T05:19:45.287 回答