首页 新闻 会员 周边

MVC如何访问不同命名空间下相同名字控制器里的Action()

0
[已解决问题] 解决于 2014-07-10 09:39

如图,我的项目里有两个名字相同的控制器IndexController,但是目录(命名空间)不一样。

路由匹配代码:

routes.MapRoute(
name: "Controller1",
url:"{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "Default", id = UrlParameter.Optional } ,
namespaces: new string[] { "MvcTest.Controllers" }
);
routes.MapRoute(
name:"Controller2",
url: "NewTest/{controller}/{action}{id}",
defaults: new { controller = "Index", action = "Test", id = UrlParameter.Optional },
namespaces: new string[] { "MvcTest.Controllers.NewTest" }
);

 

访问Default()或Test()还提示错误:

“index”请求找到下列匹配的控制器:
MvcTest.Controllers.IndexController
MvcTest.Controllers.NewTest.IndexController


请大家帮忙看看怎么回事?谢谢。

mvc
paulhe的主页 paulhe | 初学一级 | 园豆:73
提问于:2014-05-24 11:26
< >
分享
最佳答案
1

一般情况下指定了命名空间应该是可以的,不知道怎么回事,有空研究一下。

奖励园豆:5
晓菜鸟 | 老鸟四级 |园豆:2594 | 2014-07-10 09:29
其他回答(3)
0

我觉得是不行的,MVC系统在找Controller时,不考虑项目中文件夹路径的。

在路由中,是不认Controller所在的文件夹的

 

Controller外的文件夹只起便于分类、代码便于管理的作用

chutianshu_1981 | 园豆:43 (初学一级) | 2014-05-24 12:04

我上面的写法就是从网上找的,有人说可以。在看看吧

支持(0) 反对(0) paulhe | 园豆:73 (初学一级) | 2014-05-24 16:47
0

通过在routes.MapRoute()这个扩展方法里面设置namespaces: new string[] { "MvcTest.Controllers" }和namespaces: new string[] { "MvcTest.Controllers.NewTest" }是可以解决的。不过你要注意配置的路由的顺序。

JRoger | 园豆:258 (菜鸟二级) | 2014-05-24 17:25
0

两个路由规则换下位置

程序新青年 | 园豆:841 (小虾三级) | 2014-05-26 11:37

顺序换了,还是找不到路径。。。

支持(0) 反对(0) paulhe | 园豆:73 (初学一级) | 2014-05-27 12:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册