Mat*_*ete 5 attributerouting asp.net-mvc-5
我正在使用MVC属性路由(MVC 5.1.2)并遇到错误:
找到了与URL匹配的多种控制器类型.如果多个控制器上的属性路由与请求的URL匹配,则会发生这种情
请求已找到以下匹配的控制器类型:FFInfo.WebUI.Areas.Admin.Controllers.HomeController FFInfo.WebUI.Areas.Admin.Controllers.SectionController
这种情况只发生在我去的时候/Admin/Sections/,我不确定为什么因为只有一条路线可以匹配该URL,任何人都可以帮我弄清楚出了什么问题?请注意这个问题是5.1.2独有的,MVC 5.0工作正常.
基础控制器:
[RouteArea("Admin")]
public class BaseController : Controller
{
}
Run Code Online (Sandbox Code Playgroud)
家庭控制器:
[RoutePrefix("")]
[Route("{action}")]
public class HomeController : BaseController
{
public ActionResult Index()
{
}
public ActionResult Updates()
{
}
[ChildActionOnly]
public PartialViewResult GetUpdatesGrid()
{
}
public ActionResult GetUpdates(JqGridRequest Request)
{
}
}
Run Code Online (Sandbox Code Playgroud)
部门控制器:
[RoutePrefix("Sections")]
[Route("{action}")]
public class SectionController : BaseController
{
[Route]
public ActionResult Sections()
{
}
[ChildActionOnly]
public PartialViewResult GetSectionsGrid()
{
}
public ActionResult GetSections(JqGridRequest Request)
{
}
public ActionResult AddSection()
{
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult AddSection(AddEditSectionVM model, HttpPostedFileBase LogoFile)
{
}
public ActionResult EditSection(Int16? ID)
{
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult EditSection(AddEditSectionVM model, HttpPostedFileBase Logo)
{
}
public ActionResult Releases()
{
}
[ChildActionOnly]
public PartialViewResult GetReleasesGrid()
{
}
public ActionResult GetReleases(JqGridRequest Request)
{
}
public ActionResult AddRelease()
{
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult AddRelease(AddEditReleaseVM model)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我对the RouteArea RoutePrefix和Route属性的理解告诉我,/Admin/Index将调用Index ActionResultHome Controller和URL Admin/Sections应该调用Index ActionResultSections Controller.所有其他路线在每个控制器中完美运行,当你去/Admin/Index那个工作正常.我去的时候才会收到这个错误/Admin/Sections.怎么了?
小智 7
这似乎是在ASP.Net MVC 5.1重大更改相关属性路由如何处理潜在的暧昧匹配的副作用: http://www.asp.net/mvc/overview/releases/mvc51-release-notes
从5.0更新到当前的5.1.2时,我们遇到了类似的问题.看起来像这样的嵌套路线恰好是基于旧的逻辑工作,现在它们由于严格的改变而失败.
在您的示例中,/ Admin/Index在技术上可以匹配HomeController,因为它可以解释为/ {area = Admin}/{action = Index}.看起来似乎没有任何特殊逻辑(或者至少似乎没有),以查看{action}段是否恰好匹配同一区域中备用控制器上的已定义RoutePrefix.
这似乎使得这样的嵌套路由不再可能,因为你必须在HomeController中添加一个定义的RoutePrefix,如"Home",以区分控制器路由匹配.也许这可以通过RouteConstraint或其他机制解决,但我还没有找到解决方案.