C# 路径控制器未找到或未实现 IController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14011026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The controller for path was not found or does not implement IController
提问by reaper_unique
I have an MVC4 project with language selection:
我有一个带有语言选择的 MVC4 项目:
- en
- nl
- fr
- de
- 恩
- nl
- fr
- 德
1 main part with:
1个主要部分:
- About
- Common (for the menu)
- Contact
- Faq
- Home
- 关于
- 通用(用于菜单)
- 接触
- 常问问题
- 家
And 3 areas:
以及 3 个区域:
- Admin
- Customers
- Shop
- 行政
- 顾客
- 店铺
In each area I have at least one controller, for example in Admin I have the controller overviewwith the corresponding view folder overviewwhich contains an index.aspxpage.
在每个区域我至少有一个控制器,例如在 Admin 我有控制器概览和相应的视图文件夹概览,其中包含一个index.aspx页面。
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:
但是,当我按照 url: localhost:xxxx/en/admin/overview 我得到错误:
The controller for path '/en/admin/overview' was not found or does not implement IController.
路径“/en/admin/overview”的控制器未找到或未实现 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:
即使路线是正确的(我可以用 Route Debugger 看到这一点),错误页面也显示当我想加载我的主菜单项时抛出了错误:
<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.
一切似乎都井然有序,但 MVC 似乎无法加载位于主要部分的菜单。
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?
所以,问题的根源是:我可以授予一个区域(例如管理员)访问我项目的主要部分(home、common、about 等)中的控制器的权限吗?
采纳答案by reaper_unique
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
这与这里的问题完全相同: 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.
编辑:要清楚,这被标记为答案,因为它是我的问题的答案。以上答案可能会解决触发相同错误的原因。
回答by Carlos Martinez T
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.
我有一个包含多个项目的解决方案。所有项目都在MVC3中。我在我的机器上安装了Visual Studio 2012,似乎有些项目会自动升级到MVC4。
I got this problem
我遇到了这个问题
The controller for path '/etc/etc' was not found or does not implement IController
路径“/etc/etc”的控制器未找到或未实现 IController
because the project that handled that route was pointing to MVC4.
因为处理该路由的项目指向 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:
我必须手动更新他们的引用才能使用 MVC3。您也可以通过使用文本编辑器打开 .csproj 文件来实现。找到对 MVC3 的引用并删除此行:
<SpecificVersion>False</SpecificVersion>
回答by armulator
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
在做了大量的谷歌搜索之后,我发现我只需要修改我的 Global.asax.cs 文件,它就可以识别新的控制器。我所做的只是在 Global.asax.cs 文件中添加了一个空格,以便对其进行修改并使其正常工作
回答by Tejasvi Hegde
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"
在我的情况下,同样的错误与 Area 无关,但想发布在我的情况下引起的错误,这可能对通过搜索“找不到路径控制器或未实现 IController 来访问此线程的人有所帮助” ”
The error was caused because of wrong entry in _Layout.cshtml file.
该错误是由于 _Layout.cshtml 文件中的错误条目引起的。
@Styles.Render("~/Content/misc")
The bundle with that name was removed in BundleConfig.cs but forgot to remove it in _Layout.cshtml
在 BundleConfig.cs 中删除了具有该名称的包,但忘记在 _Layout.cshtml 中删除它
It was silly, but we programmers always do lot of silly mistakes :)
这很愚蠢,但我们程序员总是会犯很多愚蠢的错误:)
回答by George Stocker
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
.
这个错误也可能是由于控制器必须(在他们的名字中)有这个词Controller
;即:HomeController
; 除非你实现自己的ControllerFactory
.
回答by Richard Freeman
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 部分中的一个控制器位于错误的命名空间中。将命名空间更正为:
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.
我怀疑关键因素是让给定区域内的所有控制器共享相同的命名空间。
回答by SoaperPlus
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
回答by Carl Sharman
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
.)
(上面的命名空间应该是MySystem.Areas.Customers
.)
Will I ever learn to stop copy and pasting code? Probably not.
我会学会停止复制和粘贴代码吗?可能不是。
回答by Mike Smith - MCT - MVP
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.
此错误的另一个原因:在布局文件中意外使用了 Html.Action,其中 Html.ActionLink 可能是预期的。如果 Html.Action 引用的视图使用相同的布局文件,您实际上已经创建了一个无限循环。(布局视图将引用视图加载为局部视图,然后加载加载引用视图的布局视图...)有关堆栈大小过大的消息。
回答by Rush Frisby
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)
就我而言,我@{ Html.RenderAction("HeaderMenu", "Layout", new { Area = string.Empty }); }
在 _Layout.cshtml 中有,但 LayoutController 不存在!(我从另一个解决方案复制了 _Layout.cshtml 但忘记复制控制器)