mercredi 6 juin 2018

The controller for path was not found or does not implement IController in MVC

I am facing a problem here with MVC.

I have a _Layout.cshtml file with the following code

@{Html.RenderAction("Menus", "Menu", new { ActiveMenuId = ViewBag.ActiveMenu });}

and the action called goes like this

[ChildActionOnly]
public ActionResult Menus(int ActiveMenuId)
{
    var model = GetMenus(context, ActiveMenuId);
    return PartialView("_Menu", model);
}

model will be containing the Menu details such as ControllerName, ActionName, etc.

The partial view is like this

@model IList<App.Models.Menu>
<div class="collapse navbar-collapse" id="navbarColor01">
<ul class="navbar-nav mr-auto">
    @{
        foreach (var m in Model)
        {
            <li class="@m.ActiveClass">
                @{
                    <a href="@Url.Action(m.ActionName, m.ControllerName, new { area = m.Area })" class="nav-link"><span class="fas @m.IconClass"></span>@m.DisplayName</a>
                }
            </li>
        }
    }
</ul>

When the foreach hits the data

Models.Menu
{
    ActionName: "Index",
    ControllerName: "Home",
    Area: "Area1",
    IconClass: "someClass",
    DisplayName: "Area1",
    ActiveClass: "someClass"
}

it generates a URL /Area1

Yet somehow, if I clicked that Area1 menu, it throws an error

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

And the controller I'm calling inside the area was this one.

namespace App.Areas.Area1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Area1";
            return View();
        }
    }
}

Here is the Area1AreaRegistration.cs

namespace App.Areas.Area1
{
    public class Area1AreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "Area1";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                name: "Area1_default",
                url: "Area1/{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new [] {"App.Areas.Area1.Controllers"}
            );
        }
    }
}

And here is my RouteConfig.cs

namespace App
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "App.Controllers" }
            );
        }
    }
}

Hope you guys can help me with this one.




Aucun commentaire:

Enregistrer un commentaire