MVC 3 does not look for views under Areas

Jaco Pretorius picture Jaco Pretorius · Jul 20, 2011 · Viewed 13.5k times · Source

I'm using multiple areas in MVC 3 and I'm having problems with my views not being found. The routing seems to pick up my controllers correctly (all the actions are executing without any problems), but when I return a view MVC simply doesnt find it.

So if I have a simple controller called 'Thing' in an area called 'Some' and I do the following...

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

The action is executed correctly, but MVC doesn't find the view and I'll get a message saying something like

The view 'Index' or it's master was not found... And it will show me all the searched locations, which will be

~/Views/Thing/Index.cshtml ~/Views/Shared/Index.cshtml

etc, etc, but it doesn't look in

~/Some/Views/Thing/Index.cshtml

Any ideas on what I am doing wrong?

Answer

Jaco Pretorius picture Jaco Pretorius · Jul 21, 2011

Ok, sorry to have to answer my own question but nobody really gave me the answer I was looking for. It seems my problem was with custom routing.

To recreate the problem, I created a blank MVC 3 project and added an Area called 'Some' and a controller in that area called 'Thing'. On thing I created an Index action which simply returned a view. I then added the Index view to ~/Areas/Some/Views/Thing/Index.cshtml

Great. So when I hit /Some/Thing/Index it returns the view correctly.

Now go and add a route to Global.asax that looks like this:

routes.MapRoute(
                "Custom", // Route name
                "Bob", // URL with parameters
                new { area = "Some", controller = "Thing", action = "Index" }
                );

Now when I navigate to /Bob I get the error I mentioned - MVC doesn't find the view. To fix this problem I had to register this route in the SomeAreaRegistration class instead of Global.asax. I also didn't need the 'area' property, so it looks like this.

    context.MapRoute(
        "Custom", // Route name
        "Bob", // URL with parameters
        new { controller = "Thing", action = "Index" }
        );