加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – T4MVC和不同区域的重复控制器名称

发布时间:2020-12-16 06:57:22 所属栏目:asp.Net 来源:网络整理
导读:在我的应用程序中,我将控制器命名为Snippets,默认区域(在应用程序根目录中)和我的区域名为Manage.我使用T4MVC和自定义路由,如下所示: routes.MapRoute( "Feed","feed/",MVC.Snippets.Rss()); 我收到这个错误: Multiple types were found that match the co
在我的应用程序中,我将控制器命名为Snippets,默认区域(在应用程序根目录中)和我的区域名为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存在带有名称空间参数的重载,但是T4MVC支持没有这样的重载.可能是我错过了什么?可能的语法可以是:

routes.MapRoute(
    "Feed",MVC.Snippets.Rss(),new string[] {"Snippets.Controllers"}           
);

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

routes.MapRoute(
    "Feed",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,ActionResult result,object defaults) {
        return MapRoute(routes,defaults,null /*constraints*/,string[] namespaces) {
        return MapRoute(routes,null /*defaults*/,namespaces);
    }

    public static Route MapRoute(this RouteCollection routes,object defaults,object constraints) {
        return MapRoute(routes,constraints,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进行大的改动,我找不到明显的方法来完成这项工作.

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

谢谢!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读