asp.net - 发现了多种与名为 'Home'的 Controller 匹配的类型。 (两个区域,相同的 Controller 名称)

标签 asp.net asp.net-mvc asp.net-mvc-3

这可能对许多人来说都是重复的,但是其中明显的答案并不能解决我的问题。

我得到:

Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Home' has found the following matching controllers:
App.Web.Controllers.HomeController
App.Web.Areas.Mobile.Controllers.HomeController

我在Global.ascx.cs中为我的HomeController设置了默认 namespace :
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new string[] { "App.Web.Controllers.HomeController" } 
    );

(验证App.Web.Controllers.HomeController不是错字)。

并且还在MobileAreaRegistration中注册了Mobile的HomeController:
public override void RegisterArea(AreaRegistrationContext context) {
    context.MapRoute(
        "Mobile_default",
        "Mobile/{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

因此,为什么仍然看到错误消息?我已经盖好/打扫好了,然后又跑了。结果还是一样。

这是我注册路线的方式:
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

最佳答案

出于明显原因,请在您的Global.asax路由注册中替换:

new string[] { "App.Web.Controllers.HomeController" } 

和:
new string[] { "App.Web.Controllers" } 

那是您应在此处使用的 namespace 约束,而不是特定的类型。

关于asp.net - 发现了多种与名为 'Home'的 Controller 匹配的类型。 (两个区域,相同的 Controller 名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8956912/

相关文章:

jquery - MVC3 后不显眼的客户端验证但提交前 Hook 可用吗?

asp.net-mvc-3 - 丢失我的 session 变量

c# - 如何在 asp.net 中嵌套中继器

c# - 如何在 C# 中将 List<dynamic> 转换为 List<OurClass>

c# - 如何在 javascript 中找到父窗口的标题?

c# - .NET SMTP 客户端 - 客户端无权作为此发件人发送

asp.net - 更改 GridView 中特定记录的样式

c# - Entity Framework 选择上个月记录

asp.net-mvc - 我的 MVC 4 应用程序无法在 Azure 上加载。我如何找出问题所在?

asp.net-mvc-3 - 关于是否使用SSL时的隐藏字段加密和签名