我有一个用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
将传递Index
到的URL一起使用。
[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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句