The controller for path was not found or does not

2019-01-10 09:02发布

I have an MVC4 project with language selection:

  • en
  • nl
  • fr
  • de

1 main part with:

  • About
  • Common (for the menu)
  • Contact
  • Faq
  • Home

And 3 areas:

  • Admin
  • Customers
  • Shop

In each area I have at least one controller, for example in Admin I have the controller overview with the corresponding view folder overview which contains an index.aspx page.

The home page and all the main pages (about, faq, etc.) work and can be visited).

However, when I follow the url: localhost:xxxx/en/admin/overview I get the error

The controller for path '/en/admin/overview' was not found or does not implement IController.

Even though, the route is correct (I can see this with Route Debugger). THe error page also shows that the error was thrown when I wanted to load my main menu items:

<nav id="site-navigation" class="eightcol">
    @Html.Action("MenuItems", "Common")
</nav>

-- Code removed because irrelevant --

Everything seems to be in order, but MVC doesn't seem to be able to load the menu, which is located in the main part.

So, the root of the problem is: Can I grant an area (e.g. Admin) access to the controllers in the main part (home, common, about, etc.) of my project?

17条回答
戒情不戒烟
2楼-- · 2019-01-10 09:50

Not sure if this hits the solution from a different angle to the accepted answer, but I found that one of my controllers in the Areas section was sitting in the wrong namespace. Correcting the namespace to:

Areas.{AreaName}.Controller

fixed the issue for me.

I suspect the key factor was to have all the controllers within a given area share the same namespace.

查看更多
Deceive 欺骗
3楼-- · 2019-01-10 09:51

Yet another possible root cause for this error is if the namespace for the area registration class does not match the namespace for the controller.

E.g. correct naming on controller class:

namespace MySystem.Areas.Customers
{
    public class CustomersController : Controller
    {
        ...
    }
}

With incorrect naming on area registration class:

namespace MySystem.Areas.Shop
{
    public class CustomersAreaRegistration : AreaRegistration
    {
        ...
    }
}

(Namespace above should be MySystem.Areas.Customers.)

Will I ever learn to stop copy and pasting code? Probably not.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-10 09:55

If appropriate to your design, you can make sure the access modifier on your controller class is 'public', not something that could limit access like 'internal' or 'private'.

查看更多
\"骚年 ilove
5楼-- · 2019-01-10 09:58

This problem also occurs if you don't include your controller class for compile-process in the .csproj files.

<Compile Include="YOUR_CONTROLLER_PATH.cs" />
查看更多
走好不送
6楼-- · 2019-01-10 09:59

One other cause of this error: Accidental use of Html.Action in a Layout file where Html.ActionLink may have been intended. If the view referenced by the Html.Action uses the same Layout file you effectively have created an endless loop. (The layout view loads the referenced view as partial view which then loads the layout view which loads the referenced view...) If you set a breakpoint in the Layout file and single step through the Htlm.Action you will sometimes get a more helpful message about excessive stack size.

查看更多
地球回转人心会变
7楼-- · 2019-01-10 10:01

Embarrassingly, the problem in my case is that I haven't rebuilt the code after adding the controller.

So maybe the first thing to check is that your controller was built and is present (and public) in the binaries. It might save you few minutes of debugging if you're like me.

查看更多
登录 后发表回答