属性路由会抛出一个"找到的多个控制器类型"错误

时间:2015-06-27 21:19:52

标签: c# asp.net-mvc

我'我只是使用属性路由,真的可以'得到一些工作路线

这是我的 ProjectsController.cs 路由属性

[RouteArea("Admin")]
[Route("Projects/{action}/{id?}")]
public class ProjectsController : Controller

这是我的 RepresentativesController.cs 路由属性

[RouteArea("Admin")]
[RoutePrefix("Projects")]
[Route("Representatives/{action}/{id?}")]
public class RepresentativesController : Controller

这是文件夹结构(虽然不应该这样):

-Areas
 |-Admin
   |-Controllers
     |-ProjectsController.cs
     |-RepresentativesController.cs

我可以'设法浏览此Url例如

/Admin/Projects/Representatives/Index

因为我一直收到这个错误:

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

The request has found the following matching controller types: 
FasecItaliaLabMVC.Areas.Admin.Controllers.ProjectsController
FasecItaliaLabMVC.Areas.Admin.Controllers.RepresentativesController

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

修改路线,可能的选项:

  1. {id?}移除[Route("Projects/{action}/{id?}")],因为目前{id?}index匹配。
  2. 延长路线,例如[Route("Projects/Main/{action}/{id?}")]
  3. 删除路由前缀[RoutePrefix("Projects")]
  4. 很难说出单一的解决方案,这取决于您的控制器。