0
votes

I have a situation where I have several controllers in an administration "Area" in an ASP.NET MVC 5 application.

One of the controllers is working fine, the second controller is routing to the default route at the root of the site.

My default route:

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Reports", action = "Index", id = UrlParameter.Optional }
        );

My area route:

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

My working area controller:

 public class ResourcesController : Controller
{
    // GET: Administration/Resources
    public ActionResult Index()
    {
        using (var db = new SDRContext())
        {
            var resource = db.Resources.ToList();
            return View(resource);
        }
    }
}

My non-working area controller:

 public class DisplayFieldsController : Controller
{
    private SDRContext db = new SDRContext();

    // GET: Administration/DisplayFields
    public ActionResult Index()
    {
        var results = db.DisplayFields.ToList();
        return View(results);
    }
}

Why when I call the second controller does it default to the root route instead of the administration route I have setup?

2

2 Answers

0
votes

I'm assuming your DisplayFields controller exists in the same area folder as the first one. How are you invoking your second controller? Are you doing this from a link? I'm assuming your area is called Administration:

Html.ActionLink("Link Text", "Index", "DisplayFields", new { Area = 
"Adminstration" },null);
0
votes

Why when I call the second controller does it default to the root route instead of the administration route I have setup?

No. Routes are utilized in the order they are registered, period. I suspect if you are having problems that you have not setup your routing in the correct order in application startup. The correct order is to register your area routes before your default route.

AreaRegistration.RegisterAllAreas();
// ...
RouteConfig.RegisterRoutes(RouteTable.Routes);

If using attribute routing, you should register it before your area routes.