找不到路径控制器...或未实现IController [英] The controller for path ... was not found or does not implement IController

查看:540
本文介绍了找不到路径控制器...或未实现IController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#使用ASP.NET MVC 5编写应用程序.我需要在应用程序的右上角添加一个全局菜单.我被告知

然后我像这样从BaseController继承了我的所有控制器

public class TasksController : BaseController
{

    public ActionResult Index(int ClientId)
    {
        ...
        return View();
    }

    public ActionResult Show(int SurveyId)
    {
        ...
        return View();
    }

}

要在布局中渲染ClientsMenu,我添加了以下代码

@Html.Action("ClientsMenu", "Menus")

现在,当我运行我的应用程序时,出现以下错误

The controller for path '/Tasks/Index' was not found or does not implement IController.

当我从布局中删除@Html.Action("ClientsMenu", "Menus")时,一切正常,但是全局菜单当然不会显示.

我该怎么做才能解决此问题?

已更新 这是我从下面的评论中获得反馈后所做的事情

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动作,并将其放入其自己的控制器MenusController中.然后,您可以从视图中调用该控制器.

@Html.Action("ClientsMenu", "Menus")

在您的示例中,您没有MenusContoller,而这正是@Html.Action("ClientsMenu", "Menus")所要查找的.

Phil Haacked-Html.RenderAction和Html.Action 由另一篇文章链接的文章为您提供了一个很好的例子.

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.

So here is what I have done.

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());
        }
    }

}

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();
    }

}

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.

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();
    }

}

but still the same error

解决方案

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

In your example you don't have a MenusContoller which is what @Html.Action("ClientsMenu", "Menus") is looking for.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆