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 10:02

In my case namespaces parameter was not matching the namespace of the controller.

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new {controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "Web.Areas.Admin.Controllers" }
    );
}
查看更多
\"骚年 ilove
3楼-- · 2019-01-10 10:04

I've found it.

When a page, that is located inside an area, wants to access a controller that is located outside of this area (such as a shared layout page or a certain page inside a different area), the area of this controller needs to be added. Since the common controller is not in a specific area but part of the main project, you have to leave area empty:

@Html.Action("MenuItems", "Common", new {area="" }) 

The above needs to be added to all of the actions and actionlinks since the layout page is shared throughout the various areas.

It's exactly the same problem as here: ASP.NET MVC Areas with shared layout

Edit: To be clear, this is marked as the answer because it was the answer for my problem. The above answers might solve the causes that trigger the same error.

查看更多
ら.Afraid
4楼-- · 2019-01-10 10:04

Here is my problem and the solution that what worked for me.

I added a new controller with a single action returning a string to an existing application. But when I navigated to that controller via browser, I was getting the same error as mentioned above.

After doing lot of googling, I found out that I simply had to modify my Global.asax.cs file for it to recognize the new controller. All I did was added a space to Global.asax.cs file so that it is modified and it worked

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-10 10:05

In my case, the same error was not related to Area but thought to post the error caused in my case, which may be helpful for the people who come to this thread by searching "The controller for path was not found or does not implement IController"

The error was caused because of wrong entry in _Layout.cshtml file.

@Styles.Render("~/Content/misc")

The bundle with that name was removed in BundleConfig.cs but forgot to remove it in _Layout.cshtml

It was silly, but we programmers always do lot of silly mistakes :)

查看更多
成全新的幸福
6楼-- · 2019-01-10 10:06

Building on this answer by George, I found in my case that I had set my controller up properly as ThingController and I had a properly defined method on that controller Edit.

But.. I was referencing it in my view with

<a href="/App/ThingController/Edit" />

Where I should have been just using the name without the word controller like

<a href="/App/Thing/Edit" />

查看更多
登录 后发表回答