找到多个与名为“Home”的控制器匹配的类型的解决方案

时间:2022-05-04
本文章向大家介绍找到多个与名为“Home”的控制器匹配的类型的解决方案,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 主地址:http://localhost:3412/Home/Index

区域地址:http://localhost:3412/T200/Home/Index

解决方法: 注册路由添加命名空间(namespaces)参数 (一定要是正确的)

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                //这里很重要 一定要是正确的 命名空间 否则一样会报错 
                namespaces: new string[] { "Demo.Controllers" }
            );
        }
    }

 区域配置也需要修改

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