首页 新闻 赞助 找找看

使用webapi+attributerouting可不可以让namespace不同而controller同名?

0
悬赏园豆:10 [已解决问题] 解决于 2013-10-21 21:10

使用webapi + attributerouting 

比如

controller A-A:

namespace A {

[RoutePrefix("a1")]

  public class A: ApiController {

  }

}

 

controller B-A:

namespace B {

[RoutePrefix("b1")]

  public class A: ApiController {

  }

}

 

两个controller同名(都是A) , namespace不同(A , B) , 路由的前缀也不同 (a1 , b1) , 

可是这样就会有错误 , 请问有没有办法做到上面代码那样?

让你笑了的主页 让你笑了 | 初学一级 | 园豆:103
提问于:2013-10-21 11:08
< >
分享
最佳答案
0

目前ASP.NET Web API不支持这个场景:

In general (not with attribute routing only), we currently do not support having controllers with same name across different namespaces. We are currently looking into supporting this in the next release.

详见:Attribute Routing not working with controllers of the same name

收获园豆:10
dudu | 高人七级 |园豆:31075 | 2013-10-21 11:48

哇..dudu老大...

那下个版本...会是MVC5或WEBAPI2么..AttributeRouting不已经内置到这个版本了么....

这个具体实现应该不会太难的吧(虽然我不太会)...有比较好的解决方法么?

----

另外再问一下,Controller的类名是必须要带 XXXController 后面的Controller吧?

让你笑了 | 园豆:103 (初学一级) | 2013-10-21 13:14

@让你笑了: 

这个帖子的发布时间与回复时间是10月19日,应该指的是Web API 2的下一个版本。

默认情况下,不加Controller,就无法根据RouteTable的路由信息找到对应的Controller。

dudu | 园豆:31075 (高人七级) | 2013-10-21 13:51

@dudu: 谢谢啦

让你笑了 | 园豆:103 (初学一级) | 2013-10-21 21:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册