【问题标题】:Multiple controller types were found that match the URL Error MVC 5找到多个与 URL 错误 MVC 5 匹配的控制器类型
【发布时间】:2020-02-05 16:53:46
【问题描述】:

在我提出问题之前,我应该说一下;所有路由都已通过Route 属性添加到控制器中。它不是 thisthis 的副本。因为在这种情况下,ID参数(整数类型)被传递给了两个不同的函数。

有两个类和两个函数,它们都分离在不同的类中。导航此页面 localhost:11111/Blog/this-is-blog-title/1 时,HomeController.AppPageBlogController.Detail 函数发生冲突。我想运行 Second One,如下所述。

在第二个中,Blog 段必须稳定在路线的开头。我不想更改或删除。

感谢您的建议和帮助。

第一个

public class HomeController : BaseController
    [Route("{title}/{ID}")]              //  -> No problem with this
    [Route("{title1}/{title2}/{ID}")]    //  -> Conflicting attribute
    public ActionResult AppPage(int ID)
    {
        // Some Code
        return View();
    }
}

第二个

public class BlogController : BaseController
    [Route("Blog/{title}/{ID}")]              //  -> Conflicting attribute
    public ActionResult Detail(int ID)
    {
        // Some Code
        return View();
    }
}

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 controller routes attributes


    【解决方案1】:

    尝试将 order 参数添加到 route 属性,以便 Blog 路由优先于 title1 路由

    默认情况下,所有已定义路由的 Order 值为 0,并且路由从最低到最高处理

    public class HomeController : BaseController
        [Route("{title}/{ID}")]              //  -> No problem with this
        [Route("{title1}/{title2}/{ID}", Order = 2)]    
        public ActionResult AppPage(int ID)
        {
            // Some Code
            return View();
        }
    }
    
    public class BlogController : BaseController
        [Route("Blog/{title}/{ID}", Order = 1)]             
        public ActionResult Detail(int ID)
        {
            // Some Code
            return View();
        }
    }
    

    如果这不起作用,您可以在 RouteConfig.cs 文件中列出它们并在 title1 路由之前写博客路由

    您可以阅读这篇文章了解更多信息

    http://rion.io/2015/11/13/understanding-routing-precedence-in-asp-net-mvc/

    【讨论】:

    • 它没有用。仍然是两个功能冲突。实际上,我不想在 web.config 中列出它们。我尝试编写一个视图引擎
    • 在 route.config 而不是 web.config
    • 你的意思是 RouteConfig.cs 吗?
    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 2023-04-06
    • 2017-09-20
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多