在我的项目中遇到多个同名控制器的问题 [英] Having issue with multiple controllers of the same name in my project

查看:22
本文介绍了在我的项目中遇到多个同名控制器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ASP.NET MVC 3 项目遇到以下错误:

I am running into the following error with my ASP.NET MVC 3 project:

找到多个匹配的类型名为家"的控制器.这个可以如果服务于此的路线发生request ('Home/{action}/{id}') 确实不指定命名空间来搜索与请求匹配的控制器.如果是这种情况,请注册此通过调用重载来路由'MapRoute' 方法需要一个命名空间"参数.

Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('Home/{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.

对家"的请求已找到以下匹配控制器:MyCompany.MyProject.WebMvc.Controllers.HomeControllerMyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController

The request for 'Home' has found the following matching controllers: MyCompany.MyProject.WebMvc.Controllers.HomeController MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController

我的默认控制器文件夹中有一个 HomeController,类名为 MyCompany.MyProject.WebMvc.Controllers.HomeController.

I have a HomeController in my default controller folder, with a class name of MyCompany.MyProject.WebMvc.Controllers.HomeController.

在我的 global.asax 中,我的 RegisterRoutes 方法如下所示:

My RegisterRoutes method, in my global.asax, looks like:

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

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

然后我有一个名为 Company 的区域,在该区域的默认控制器文件夹中有一个 HomeController,类名为 MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController.

I then have an area called Company, with a HomeController in the default controller folder for the area, with a class name of MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController.

CompanyAreaRegistration 文件中的 RegisterArea 方法如下所示:

The RegisterArea method in the CompanyAreaRegistration file looks like:

   public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Company_default",
            "Company/{controller}/{action}/{id}",
            new { area = "Company", action = "Index", id = UrlParameter.Optional }
        );
    }

这都是导致我在本文开头强调的错误的原因.我正在努力尝试从其他各种帖子中拼凑出一个解决方案,运气不佳.

This is all leading the error I highlighted at the beginning of this post. I am struggling trying to piece together a solution from various other posts, with NO LUCK.

是否可以在默认控制器文件夹中有一个 HomeController,然后在每个区域中有一个?如果是这样,我是否需要(假设我这样做)更改我的配置文件才能使其正常工作?

Is it possible to have a HomeController in the default controllers folder and then one in EACH area? If so, do I need to make (assuming I do) changes to my configuration file to make this work?

任何帮助将不胜感激!

推荐答案

该错误消息包含推荐的解决方案:如果是这种情况,请通过调用采用命名空间"的MapRoute"方法的重载来注册此路由' 参数."

The error message contains the recommended solution: "If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter."

routes.MapRoute(
     "Default", // Route name
     "{controller}/{action}/{id}", // URL with parameters
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
     new string[] { "MyCompany.MyProject.WebMvc.Controllers"}
);

这将使 http://server/ 转到您的 HomeController 的 Index 操作,我认为这就是您想要的.http://server/company/home 会去Company区域的HomeController的Index动作,在区域注册中定义.

This will make http://server/ go to your HomeController's Index action which is, I think, what you want. http://server/company/home will go to the Company area's HomeController's Index action, as defined in the area registration.

这篇关于在我的项目中遇到多个同名控制器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆