Multiple types were found that match the controlle

2019-01-03 20:17发布

I currently have two unrelated MVC3 projects hosted online.

One works fine, the other doesn't work, giving me the error:

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 way my hoster works is that he gives me FTP access and in that folder I have two other folder, one for each of my applications.

ftpFolderA2/foo.com

ftpFolderA2/bar.com

foo.com works fine, I publish my application to my local file system then FTP the contents and it works.

When I upload and try to run bar.com, the issue above fires and prevents me from using my site. All while foo.com still works.

Is bar.com searching from controllers EVERYWHERE inside of ftpFolderA2 and that's why it's finding another HomeController? How can I tell it to only look in the Controller folder as it should?

Facts:

  1. Not using areas. These are two COMPLETELY unrelated projects. I place each published project into each respective folder. Nothing fancy.
  2. Each project only has 1 HomeController.

Can someone confirm this is the problem?

24条回答
乱世女痞
2楼-- · 2019-01-03 20:22

Even though you are not using areas, you can still specify in your RouteMap which namespace to use

routes.MapRoute(
    "Default",
    "{controller}/{action}",
    new { controller = "Home", action = "Index" },
    new[] { "NameSpace.OfYour.Controllers" }
);

But it sounds like the actual issue is the way your two apps are set up in IIS

查看更多
我命由我不由天
3楼-- · 2019-01-03 20:23

Got same trouble and nothing helped. The problem is that I actually haven't any duplicates, this error appears after switching project namespace from MyCuteProject to MyCuteProject.Web.

In the end I realized that source of error is a global.asax file — XML markup, not .cs-codebehind. Check namespace in it — that's helped me.

查看更多
4楼-- · 2019-01-03 20:24

You can also get the 500 error if you add your own assembly that contains the ApiController by overriding GetAssemblies of the DefaultAssembliesResolver and it is already in the array from base.GetAssemblies()

Case in point:

public class MyAssembliesResolver : DefaultAssembliesResolver
{
    public override ICollection<Assembly> GetAssemblies()
    {
        var baseAssemblies = base.GetAssemblies();

        var assemblies = new List<Assembly>(baseAssemblies);

        assemblies.Add(Assembly.GetAssembly(typeof(MyAssembliesResolver)));

        return new List<Assembly>(assemblies);
    }
}

if the above code is in the same assembly as your Controller, that assembly will be in the list twice and will generate a 500 error since the Web API doesn't know which one to use.

查看更多
男人必须洒脱
5楼-- · 2019-01-03 20:26

Watch this... http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas

Then this picture (hope u like my drawings)

enter image description here

查看更多
贪生不怕死
6楼-- · 2019-01-03 20:26

Check the bin folder if there is another dll file that may have conflict the homeController class.

查看更多
The star\"
7楼-- · 2019-01-03 20:26

i was facing the similar issue. and main reason was that i had the same controller in two different Area. once i remove the one of them its working fine.

i have it will helpful for you.

Project Solution

查看更多
登录 后发表回答