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

art*_*olk 5 asp.net-mvc t4mvc asp.net-mvc-2

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

routes.MapRoute(
    "Feed",
    "feed/",
    MVC.Snippets.Rss()
);
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

找到了多个匹配名为"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"}           
);
Run Code Online (Sandbox Code Playgroud)

或者,将名称空间作为T4MVC属性对我来说似乎相当不错:

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

提前致谢!

Dav*_*bbo 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;
    }
Run Code Online (Sandbox Code Playgroud)

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

 string[] { typeof(MyApplication.Controllers.SnippetsController).Namespace }
Run Code Online (Sandbox Code Playgroud)

我应该理想地添加它,你根本不需要传递命名空间,因为你在MVC.Snippets.Rss()调用中已经捕获了定位特定控制器的意图.但是,如果没有对T4MVC进行大的改动,我找不到明显的方法来完成这项工作.

无论如何,请检查并测试更改,并让我知道它是如何工作的.如果它看起来不错,我会明白的.

谢谢!