2

A new project is unable to find the views for a new Area.

It is trying to find it in the parent Views folder.

A suggested solution is to put .DataTokens.Add("area", "AREANAME") into the RegisterArea method.

public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Test_default",
                "Test/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            ).DataTokens.Add("area", "Test");
        }

However, with this, I'm getting an "An item with the same key has already been added." error.

UPDATE 1: I'm not doing anything fancy on the controller.

public ActionResult Index()
        {
            return View();
        }

UPDATE 2: I just would like to add that I'm also registering components into a Unity container. I don't think this is causing issues, though, because it explicitly states that it is not necessary to register my controllers.

Jonas Arcangel
  • 2,085
  • 11
  • 55
  • 85

3 Answers3

5

It's most likely because the View is being searched from namespace

YourNamespace.YourApplication.Controllers 

and instead it should be looked from namespace (or what ever namespace you have defined)

YourNamespace.YourApplication.Areas.Test.Controllers

Add following to your route declaration

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Test_default",
        "Test/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "YourNamespace.YourApplication.Areas.Test.Controllers" }
    );
}
Janne Matikainen
  • 5,061
  • 15
  • 21
1

The method Register Area already know you are in your Area named Test. Using the function DataTokens.Add("area", "Test") declares a second time your route. That's why you get this error.

The initial issue for your view is certainly not related to your routing settings. Is there a custom code when rendering the view from the controller ?

Jämes
  • 6,945
  • 4
  • 40
  • 56
1

I've found that if your route declaration isn't above the "catch all" route declaration (usually your parent views as you pointed out), it will default to the parent each time. Try moving the route declaration above anything the parent will try to catch

user3036342
  • 1,023
  • 7
  • 15