文章内容

2017/4/13 15:34:24,作 者: 黄兵

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

最近在写代码的时候报如下错误:

找到多个与名为“Home”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配的控制器,则会发生这种情况。如果是这样,请通过调用带有 'namespaces' 参数的 "MapRoute" 方法的重载来注册此路由。
“Home”请求找到下列匹配的控制器:
_8677333.com.Areas.Topics.Controllers.HomeController
_8677333.com.Controllers.HomeController

在网上查找了一下资料,解决了问题,解决方案如下;

一、产生问题原因:

最主要是在一个项目中同时存在两个为Home的Controll,如下截图:


二、解决方法:

1:Area下的XXXAreaRegistration 添加:new string[] { "xxx.Areas.xxx.Controllers" }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System.Web.Mvc;
 
namespace _8677333.com.Areas.Topics
{
    public class TopicsAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "Topics";
            }
        }
 
        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Topics_default",
                "Topics/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                new string[] { "_8677333.com.Areas.Topics.Controllers" }
            );
        }
    }
}
    

2:RouteConfig 下添加 namespaces: new string[] { "xxx.Controllers" }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System.Web.Mvc;
using System.Web.Routing;
using System;
 
namespace _8677333.com
{
    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[] { "_8677333.com.Controllers" }
            );          
        }
    }
}

有什么问题,在下面留言。

转载请注明出处,谢谢!

分享到:

发表评论

评论列表