T4MVC and duplicate controller names in different areas

爱⌒轻易说出口 提交于 2019-12-05 09:03:27

Makes sense. I guess you're just the first one to run into this. Try replacing all the MapRoute methods in T4MVC.tt by the following:

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

Note that you can get strong typing on the controller namespace without T4MVC's help simply by writing:

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

I should add that ideally, you would not have to pass the Namespaces at all, since your intent to target a specific controller is already captured in the MVC.Snippets.Rss() call. However, I couldn't find an obvious way to make this work without big changes to 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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!