【问题标题】:RouteArea and "Multiple controller types were found that match the URL" errorRouteArea 和“找到与 URL 匹配的多个控制器类型”错误
【发布时间】:2015-08-14 17:39:03
【问题描述】:

假设我的 MVC 应用中有这个主控制器:

[RouteArea("Blog")]
public class PageController : BaseAppController
{
}

[RouteArea]
// Called on mydomain/Blog
// and also called on mydomain/
public class BaseAppController : Controller
{
    [Route]
    public ActionResult Index()
    {
        return Content("this is the index file form the main controller");
    }
}

正如预期的那样,我将在mydomain/Blog 上获得Index 操作,但由于某种原因,我也在我的/ 中获得它,它与我从另一个控制器获得的另一个视图相冲突。 我没有为我的任何根设置任何默认设置:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.LowercaseUrls = true;
    routes.MapMvcAttributeRoutes(new CustomDirectRouteProvider());

}

知道有什么问题吗?

【问题讨论】:

  • 什么是 CustomDirectRouteProvider?
  • @AlexArt。那是为了启用控制器继承。在这里查看:stackoverflow.com/questions/19989023/…
  • @Hooman - 根据该答案下方的 cmets,许多人无法使其正常工作。此外,答案是针对 WebAPI,而不是针对 MVC
  • @NightOwl888 谢谢,但事实并非如此。请查看答案

标签: asp.net-mvc model-view-controller routing


【解决方案1】:

它出现了,我需要将基类设置为abstract,因为它位于层次结构的顶部,因此其他视图无法访问它的任何其他实例。 所以在上面的例子中:

[RouteArea("Blog")]
public class PageController : BaseAppController
{
}

[RouteArea]
// is going to be called just on mydomain/Blog and not on mydomain/
public abstract class BaseAppController : Controller
{
    [Route]
    public ActionResult Index()
    {
        return Content("this is the index file form the main controller");
    }
}

【讨论】:

    猜你喜欢
    • 2020-02-05
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多