加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-5 – MVC AttributeRoute似乎忽略了RoutePrefix并导

发布时间:2020-12-16 07:12:58 所属栏目:asp.Net 来源:网络整理
导读:我正在使用MVC属性路由(MVC 5.1.2)并遇到错误: 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 control
我正在使用MVC属性路由(MVC 5.1.2)并遇到错误:

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

我对RouteArea RoutePrefix和Route属性的理解告诉我/ Admin / Index将调用Home Controller的Index ActionResult,URL Admin / Sections应该调用Sections Controller的Index ActionResult.所有其他路线在每个控制器中都能正常工作,当您进入/ Admin / Index工作正常时.当我进入/ Admin / Sections时,我只收到此错误.怎么了?

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读