路径的控制器 ... 未找到或未实现 IController [英] The controller for path ... was not found or does not implement IController
问题描述
我正在使用 c# 编写使用 ASP.NET MVC 5 的应用程序.我需要在应用程序的右上角添加一个全局菜单.我被告知 其他 SO 使用带有 ChildActionOnly
属性的操作.
I am writing an application using ASP.NET MVC 5 using c#. I have a need to add a global menu on the upper right hand side of the application. I was advised other SO to use action with ChildActionOnly
attribute.
这就是我所做的.
我创建了一个这样的 BaseController
I created a BaseController like this
public class BaseController : Controller
{
[ChildActionOnly]
public ActionResult ClientsMenu()
{
using (SomeContext db = new SomeContext())
{
return PartialView(db.Database.SqlQuery<Client>("SELECT * FROM clients").ToList());
}
}
}
然后我像这样从 BaseController
继承了我所有的控制器
Then I inherited all my controllers from BaseController
like so
public class TasksController : BaseController
{
public ActionResult Index(int ClientId)
{
...
return View();
}
public ActionResult Show(int SurveyId)
{
...
return View();
}
}
为了在我的布局中呈现 ClientsMenu
,我添加了以下代码
To render the ClientsMenu
in my layout I added the following code
@Html.Action("ClientsMenu", "Menus")
现在当我运行我的应用程序时出现以下错误
Now when I run my application I get the following error
The controller for path '/Tasks/Index' was not found or does not implement IController.
当我从布局中删除 @Html.Action("ClientsMenu", "Menus")
时,一切正常,但全局菜单当然不显示.
When I remove @Html.Action("ClientsMenu", "Menus")
from the layout everything works fine but the global menu does not show of course.
我该怎么做才能解决这个问题?
What can I do to resolve this issue?
已更新这是我从下面的评论中获得反馈后所做的
Updated Here is what I have done after the feedback I got from the comments below
public class TasksController : Controller
{
[ChildActionOnly]
public ActionResult ClientsMenu()
{
using (SomeContext db = new SomeContext())
{
return PartialView(db.Database.SqlQuery<Client>("SELECT * FROM clients").ToList());
}
}
public ActionResult Index(int ClientId)
{
...
return View();
}
public ActionResult Show(int SurveyId)
{
...
return View();
}
}
还是一样的错误
推荐答案
从 BaseController
中取出 ClientMenus
Action 并将其放入自己的控制器 MenusController代码>.然后,您可以从您的视图中调用该控制器.
Take ClientMenus
Action out of the BaseController
and put it into its own controller MenusController
. You can then call that controller from your Views.
@Html.Action("ClientsMenu", "Menus")
在您的示例中,您没有 @Html.Action("ClientsMenu", "Menus")
正在寻找的 MenusContoller
.
In your example you don't have a MenusContoller
which is what @Html.Action("ClientsMenu", "Menus")
is looking for.
Phil Haacked - Html.RenderAction 和 Html.Action 另一篇文章链接的文章为您提供了一个很好的例子.
The Phil Haacked - Html.RenderAction and Html.Action article linked to by the other post provided a good example for you to follow.
这篇关于路径的控制器 ... 未找到或未实现 IController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!