2

I automatic register routes for modules and trying to register routes for admin. For modules registration working fine

public class ModulSetup : BaseModule
{
    public override void Setup()
    {
        this.SetupViewEngine();
        this.SetupControllers();
    }

    public override void SetupRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "Default",
            string.Empty,
            new { controller = "Home", action = "Index" });
    }

    protected override void SetupViewEngine()
    {
        ViewEngines.Engines.Add(new CoreViewEngine());
    }

    protected override void SetupControllers()
    {
        ControllerBuilder.Current.DefaultNamespaces.Add("SGN.Core.Controllers");
    }
}

But for admin, I get 404

public class AdminSetup : BaseModule
{
    public override void Setup()
    {
        this.SetupViewEngine();
        this.SetupControllers();
    }

    public override void SetupRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "Admin",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Site", action = "Index" });

    protected override void SetupViewEngine()
    {
        ViewEngines.Engines.Add(new AdminViewEngine());
    }

    protected override void SetupControllers()
    {
        ControllerBuilder.Current.DefaultNamespaces.Add("SGN.Core.Admin.Controllers");
    }
}

Example. I have controllers class Site

namespace SGN.Core.Admin.Controllers
{
using System.Web.Mvc;
using SGN.Framework.Controller;

public class SiteController : AdminController
{
    public ActionResult Index()
    {
        return this.View();
    }
}
}

And when I try go to http://localhost:777/Admin/Site/ server return 404 :(

UPDATE

I try create AreaAwareViewEngine ho write thee How to set a Default Route (To an Area) in MVC

But not help

UPDATE

I try use this url http://localhost:777/Admin/Site/Index work. But this not good. And Master not work.

UPDATE

I use RouteDebugger for check in other project where I use Area. What add when use Area. How I understand in DataTokens Add 3 parameters

Namespaces = SGN.Web.Areas.Admin., area = Admin, UseNamespaceFallback = False*

I try add this parameters

Namespaces = SGN.Core.Admin., area = Admin, UseNamespaceFallback = False*

But not help

UPDATE

I created the class AreaAwareViewEngine how write here How to set a Default Route (To an Area) in MVC

My class AreaRegistration

namespace SGN.Web.Admin
  {
using System.Web.Mvc;

public class AdminAreaRegistration : AreaRegistration
   {
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new {area="Admin", controller = "Site", action = "Index", id = UrlParameter.Optional }
        );
    }
}
}

And create AdminViewEngine from AreaAwareViewEngine

namespace SGN.Core.Admin
{
using System.Web.Mvc;

using SGN.Framework;

public class AdminViewEngine : AreaAwareViewEngine
{
    public AdminViewEngine()
    {
        AreaMasterLocationFormats = new[] { "~/Admin/Views/Shared/admin.cshtml" };

        AreaPartialViewLocationFormats = new[] { "~/Admin/Views/{1}/Partials/{0}.cshtml", "~/Admin/Menu/{0}.cshtml" };

        AreaViewLocationFormats = new[] { "~/Admin/Views/{1}/{0}.cshtml", "~/Admin/Menu/{0}.cshtml" };

        ViewLocationFormats = AreaViewLocationFormats;
        PartialViewLocationFormats = AreaPartialViewLocationFormats;
        MasterLocationFormats = AreaMasterLocationFormats;
    }

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
    {
        return new RazorView(controllerContext, partialPath, null, false, FileExtensions);
    }

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        if (string.IsNullOrEmpty(masterPath))
        {
            masterPath = AreaMasterLocationFormats[0];
        }
        var view = new RazorView(controllerContext, viewPath, masterPath, true, FileExtensions);
        return view;
    }
}
  }

Work if i go to http://localhost:777/Admin/Site/ and not work if I go to http://localhost:777/Admin

Community
  • 1
  • 1
Hryhorii
  • 875
  • 2
  • 13
  • 36

2 Answers2

0

It could be a simple ordering issue. Are your Admin routes added before your Core routes? I believe that it will take the first matching route and unless your Admin route is added before the others, I suspect one of them is matching and it is being redirected to an Admin controller (which doesn't exist). You might want to look at it with Haack's RouteDebugger.

Also, is there any reason you're not using the built-in areas feature? This seems like a perfect match for that.

tvanfosson
  • 490,224
  • 93
  • 683
  • 780
0

First off, ensure that your "Admin" route is mapped before your default non-Admin route. Also, I'd change your Admin route to this:

routes.MapRoute(
        "Admin",
        "Admin/{controller}/{action}/{id}",
        new { controller = "Site", action = "Index", id = UrlParameter.Optional });

That way, the id route parameter has a default value of being UrlParameter.Optional. Your initial route didn't have a default for id, so it would be requiring it.