zoukankan      html  css  js  c++  java
  • mvc area出现“找到多个与名为“Home”的控制器匹配的类型”错误的解决方法

    找到多个与名为“Home”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由在搜索匹配此请求的控制器时没有指定命名空间,则会发生此情况。如果是这样,请通过调用含有“namespaces”参数的“MapRoute”方法的重载来注册此路由。”

    出现该问题的原因是在默认的Golbal.asax.cs文件中已经注册了默认路由

        public class MvcApplication : System.Web.HttpApplication
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                //routes.MapRoute(
                //    "Default", // 路由名称
                //    "{controller}/{action}/{id}", // 带有参数的 URL
                //    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
                //);

                routes.MapRoute(
                      "Member",
                      "Member/{action}/{page}",
                      new { controller = "MemberCenter", action = "List" },
                      new { action = @"Index|List|Detail", page = @"\d+" }
                    );

                routes.MapRoute(
                      "Default",
                      "{controller}/{action}/{id}",
                      new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                      new string[] { "MvcStudyDemo.Controllers" }
                      );

            }

    而在AREAS中有注册了一个同名的Controller

            public override void RegisterArea(AreaRegistrationContext context)
            {
                context.MapRoute(
                    "Order_default",
                    "Order/{controller}/{action}/{id}",
                    new { action = "Index", id = UrlParameter.Optional },
                    new string[] { "MvcStudyDemo.Areas.Order.Controllers" }
                  
                );
            }

    解决方法就是在每个注册Router的时候加上命名空间即可。

  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/zcm123/p/3096016.html
Copyright © 2011-2022 走看看