asp.net-mvc - T4MVC 和不同区域的重复 Controller 名称

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

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

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

我得到这个错误:

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



我知道 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}           
);

提前致谢!

最佳答案

说得通。我猜你只是第一个遇到这种情况的人。尝试将 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 的帮助,您只需编写以下代码即可在 Controller 命名空间上获得强类型:
 string[] { typeof(MyApplication.Controllers.SnippetsController).Namespace }

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

无论如何,请查看并测试更改,然后让我知道它是如何为您工作的。好看的话我就入手了

谢谢!

关于asp.net-mvc - T4MVC 和不同区域的重复 Controller 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2794163/

相关文章:

c# - Razor 页面路由与 Web API 中的方式相同

asp.net-mvc - 强类型 View - 通过编辑/创建表单发布附加数据

c# - Html.EditorFor 全局模板?

asp.net-mvc-3 - 如何让 HTML.Partial 使用 T4MVC 常量?

c# - 我在哪里可以获得 ASP.NET MVC 中的这个 "MVC"类

c# - 在 Mvc.Controller 中使用服务器

asp.net-mvc - 在捆绑了 Bootstrap 的 ASP.NET MVC 应用程序中使用 Glyphicons

asp.net-mvc - 在 MVC 3 项目中使用 T4MVC 生成的代码编译错误

asp.net-mvc - .NET MVC MultiSelectList 和选定的值

deployment - Crystal报表文件未部署到ASP.NET MVC中的服务器