The controller for path was not found or does not implement IController
Asked Answered
P

24

84

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?

Pub answered 23/12, 2012 at 12:40 Comment(4)
Have you tried commenting out the call to Html.Action to isolate the problem to the specific controller?Powerdive
I hadn't yet, but now I did and it's as I thought. The Area Admin uses the the layout from the main part of my project and when it reaches the Action the area tries to access the controller that isn't located in the area which throws the error. In other words, the root of the problem is, how can I grant an area access to the controllers in the main part (home, common, about, etc. controllers) of my project?Pub
The question is getting more possible problems and solutions. So it's a must, I think, to look at other answers as well and not the only the accepted one.Ethbin
I got this message when relative image references in CSS were breaking because of StyleBundlesRaimes
P
129

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.

Pub answered 23/12, 2012 at 14:5 Comment(3)
Thanks, that worked for me. It's a very misleading error message from .NET saying that the controller for the action within the area can't be found, which isn't the case at all!Aeriell
This fixed my issue. I guess next time I should start with the green check answer instead of at the top of the list.Scleroderma
'@Html.Action("About", "Home", new { area = "" })' throws compilation error when specifying it as blank area. Any suggestions ?Cutright
N
19

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 :)

Northumbria answered 13/2, 2014 at 12:25 Comment(1)
Thanks for the solution. It's indeed possible that there are multiple solutions to a seemingly identical but in actuallity only a similar problem. However, as my question stated that I use areas my own selected solution is still the most accurate.Pub
S
16

Also, for those who the solution above didn't work, here's is what worked for me:

I have a solution with multiple projects. All projects were in MVC3. I installed Visual Studio 2012 in my machine and it seems that some projects were automatically upgraded to MVC4.

I got this problem

The controller for path '/etc/etc' was not found or does not implement IController

because the project that handled that route was pointing to MVC4.

I had to manually update their references to use MVC3. You can also do that by opening the .csproj file with a text editor. Find the reference to MVC3 and remove this line:

<SpecificVersion>False</SpecificVersion>
Symposiarch answered 1/11, 2013 at 15:24 Comment(2)
Oh my gosh. That's so random and obscure. Thanks a lot for posting! :) FUTURE READERS: Mine was only slightly different - the dll version itself was 4.0 instead of 3.0 - not sure how it got changed.Margerymarget
The issue with mine was similar; I had a shared library which was referenced by the presentation layer and contained a shared controller, and the shared library was using mvc4, whereas the presentation layer was using mvc3. Once i updated the shared library to MVC3, it resolved the issue.Adhern
D
16

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.

Dissociate answered 2/12, 2015 at 16:5 Comment(1)
This caught me out and was hard to find because the namespace was wrong when it was in the root controller folder (code copied from another project), but it didn't matter before I moved it to an area.Owner
M
13

This error can also be caused by the fact that Controllers must have (in their name) the word Controller; viz: HomeController; unless you implement your own ControllerFactory.

Marsupium answered 27/1, 2015 at 21:26 Comment(2)
I had this problem. It was because I deleted my HomeController.cs file and I didn't understand what depended on it.Wendy
You are the CEOBrevier
J
13

in my case, the problem was that the controller class has not been publicly announced.

class WorkPlaceController : Controller

the solution was

public class WorkPlaceController : Controller
Jap answered 22/5, 2015 at 15:21 Comment(0)
M
7

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

Monarchism answered 7/11, 2013 at 15:37 Comment(1)
We have 3 production web servers and 5 dev/test machines. Only on one production machine was it not working. Adding a space and saving Global.asax fixed it. #mindblownHalation
V
7

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" }
    );
}
Victualer answered 3/6, 2016 at 15:45 Comment(0)
Q
6

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.

Quartermaster answered 18/2, 2015 at 7:22 Comment(1)
Wish you could favorite answers and not just questions. I've re-stumbled upon this answer so many times. It's been just so helpful, thanks.Platinum
P
5

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.

Puduns answered 14/12, 2015 at 2:19 Comment(0)
T
5

In my case I had @{ Html.RenderAction("HeaderMenu", "Layout", new { Area = string.Empty }); } in _Layout.cshtml but the LayoutController did not exist! (I had copied _Layout.cshtml from another solution but forgot to copy the controller)

Tollgate answered 6/1, 2016 at 19:28 Comment(0)
D
5

In my case in global.asax/application_start method, I was registering web api routes AFTER mvc routes like so:

RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);

Reverting the order fixed the issue

GlobalConfiguration.Configure(WebApiConfig.Register);            
RouteConfig.RegisterRoutes(RouteTable.Routes);
Delldella answered 30/1, 2019 at 8:6 Comment(0)
B
4

This could be because the path was wrong. So check the path and the spelling of the controller first. In my case, my controller was named CampsController, and the WebApiConfig.cs file had an extra path in it.

Instead of: http://localhost:6600/Camps

It was: http://localhost:6600/api/Camps

I had not noticed the api word in the WebApiConfig.cs file:

config.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "api/{controller}/{id}",
          defaults: new { id = RouteParameter.Optional }
      );

Also it could be because the controller was incorrectly named. Here I called LayoutController, but should have called Layout instead:

<a class="nav-link" href="#">@Html.Action("GetCurrentUser", "LayoutController" })</a>

should be:

<a class="nav-link" href="#">@Html.Action("GetCurrentUser", "Layout")</a>

Another example, it could be because you have bad Route paths defined. Make sure your paths are correct. Example:

   [RoutePrefix("api/camps")]
   public class CampsController : ApiController

   [Route("{moniker}")]
   public async Task<IHttpActionResult> Get(string moniker)
Blub answered 3/7, 2019 at 16:40 Comment(0)
S
3

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'.

Scope answered 21/6, 2016 at 7:7 Comment(0)
A
3

I hope this helps someone else. I had this problem because, while I had the controller named properly, the class inside the file had a typo in it. I was looking for OrderSearch and the file was OrderSearchController.cs, but the class was OrdersSearchController.

Obviously, they should match, but they don't have to, and your route targets the class, not the filename.

Alard answered 6/1, 2021 at 19:23 Comment(0)
O
2

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.

Osanna answered 1/3, 2017 at 11:25 Comment(1)
Of course that was my problem... It's always the stupid things that get you.Curculio
O
2

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" />

Occupational answered 18/9, 2018 at 9:26 Comment(0)
R
2

Somebody added this to a View.

@Scripts.Render("~/bundles/jqueryval")

Then they added the BundleConfig.cs file in the App_Start Folder.
In the RegisterBundles Method they had:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js"));

However, they forgot to finish wiring this up in the Global.asax.cs File.
To Fix, all I had to do was add this to the Application_Start Method in Global.asax.cs:

RouteConfig.RegisterRoutes(RouteTable.Routes);

Note: I think the ordering/placement of this Line in the Application_Start Method matters,
so please keep that in mind.
I placed mine immediately after ViewEngines.

Roseboro answered 17/12, 2020 at 15:47 Comment(0)
D
1

In another scenario just I would like to add is In my scenario, the name space was different for controller as it was mistake of copying controller from another project.

Dicker answered 18/2, 2016 at 7:11 Comment(0)
S
1

In my case of legacy application, the issue occurred when I added below entry in web.config file under the node <system.webServer>

       <modules runAllManagedModulesForAllRequests="true"></modules>

When I removed it, the issue resolved.

Staciestack answered 19/12, 2017 at 13:56 Comment(0)
S
1

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" />
Sg answered 13/7, 2018 at 13:5 Comment(0)
N
1

In my case, I was rendering another action method for my menu section in _layout.cshtml file using @Html.Action("Menu", "Menu") whereas I forgot to create Menu controller and as layout file was being used in my current controller action's view therefore I was getting this error in my current action render request. try to look in your layout and as well as view file if you did the same mistake

Nitpicking answered 18/9, 2019 at 11:2 Comment(0)
M
1

Or maybe you missed keyword "Controller" at the end of controller name ;)

Mettle answered 19/12, 2020 at 9:3 Comment(0)
E
1

For me, I was running the IIS Express https://localhost:1234/pg/Abc/Page But register the web in IIS like https://localhost:1234/Abc/Page So in calling web page I was calling the first link with /pg/ which is throwing Not Found Exception.

Egidius answered 2/6, 2021 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.