我有一个在MVC 5中开发的网站,我正在使用路由属性进行路由.我已经设置了 默认的控制器和该 默认操作使用下面的代码为每个控制器
public class CompanyController : MainController { [Route("~/", Name = "default")] [Route("Company/Index")] public ActionResult Index(string filter = null) { //My code here } [Route("Company/Edit")] public ActionResult Edit(int id) { //My code here } }
我有一个默认操作的另一个控制器:
[RoutePrefix("Analyst")] [Route("{action=Index}")] public class AnalystController : MainController { [Route("Analyst/Index")] public ActionResult Index(string filter = null) { //My code here } [Route("Analyst/Edit")] public ActionResult Edit(int id) { //My code here } }
默认控制器运行正常,但是当我导航到分析器控制器而未指定操作的名称时,我收到以下错误:
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: SurveyWebsite.Controllers.AnalystController SurveyWebsite.Controllers.CompanyController
如何更正导航到http:// localhost:61534/analyst并达到默认操作(索引)?该操作也应该由http:// localhost:61534/analyst/Index保持可访问 感谢您的帮助.
给出一个空字符串作为索引操作的路由值,以便它适用于Analyst,这是您的控制器路由前缀.您可以使用第二个Route
属性进行装饰,以使用" Analyst/Index
"url,您将Index
向其传递" ".
[RoutePrefix("Analyst")] public class AnalystController : MainController { [Route("")] [Route("Index")] public ActionResult Index(string filter = null) { //My code here } [Route("Edit/{id}")] public ActionResult Edit(int id) { //My code here } }
这将适用于/Analyst
和/Analyst/Index