【问题标题】:MVC AttributeRoute appears to be ignoring RoutePrefix and causing matching action in mutiple Controllers errorMVC 属性路由似乎忽略了 RoutePrefix 并导致多个控制器错误中的匹配操作
【发布时间】:2014-05-21 00:40:32
【问题描述】:

我正在使用 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
{
}

家庭控制器:

[RoutePrefix("")]
[Route("{action}")]
public class HomeController : BaseController
{
    
    public ActionResult Index()
    {
    }

    public ActionResult Updates()
    {
    }

    [ChildActionOnly]
    public PartialViewResult GetUpdatesGrid()
    {
    }

    
    public ActionResult GetUpdates(JqGridRequest Request)
    {
    }
}

部分控制器:

[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)
    {
    }
}

我对@9​​87654325@RoutePrefixRoute属性的理解告诉我/Admin/Index会调用Home Controller的IndexActionResult,而URLAdmin/Sections应该调用Index部分控制器的ActionResult。所有其他路由在每个控制器中都运行良好,当您转到 /Admin/Index 时,运行良好。当我转到/Admin/Sections 时,我只会收到此错误。怎么了?

【问题讨论】:

    标签: asp.net-mvc-5 attributerouting


    【解决方案1】:

    这似乎是 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 或其他机制解决,但我还没有找到解决方案。

    【讨论】:

    • 从 5.0 到 5.1 发生了重大变化我打开了一个 bug 票,得到的响应是“按设计工作”。
    【解决方案2】:

    我认为您需要将每个控制器上的 [Route({action})] 更改为 [Route({action=Index})]

    此外,您提到在您的SectionController 上有一个Index 操作,但我在您的代码中没有看到它。我确实看到您有一个Sections 操作,上面列出了[Route]。我猜Sections 操作实际上是您转到/Admin/Sections 时想要得到的,在这种情况下,您应该删除Sections 操作上的[Route] 并更改[Route({action})] SectionController[Route({action=Sections})]

    希望对您有所帮助。 ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-11
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 2015-11-02
      相关资源
      最近更新 更多