Reputation: 1305
This question appears on Stack Overflow many times but none of the accepted answers are working. I am trying to implement areas in a project. Even though routing is set up properly (my controllers are firing, I can step through them in the VS debugger), when the controller attempts to return a view the code fails with the infamous error I've screen snapshotted below.
Yes, my views are in the correct location and are named to match my controllers (see snapshots and code samples) and my routing seems to be set up correctly (please see code sample). The only way I can get the view to show w/o error is to hardcode full path to the view, including the area name. This error occurs even on a new, straight "out of the box" MVC sample app created with a single area. Areas just don't seem to work without specifying a full path to the view.
Any help appreciated.
My project looks like this:
My controller code. A breakpoint set in the Index method fires as expected, so the controller is being called. The line that's commented out is the only way a view is ever returned. As shown here, call fails with the error shown at the end of this question:
public ActionResult Index()
{
return View();
// return View("~/Areas/Governance/Views/ChangeDashboard/Index.cshtml");
}
Route setup: When I set up the area named "Governance" in VS, I got the following in a new "GovernanceAreaRegistration.cs" file:
public override string AreaName
{
get
{
return "Governance";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Governance_default",
"Governance/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
The error: Even though I've followed (I think) all the rules, I get the following error when I run. It doesn't appear that the Views subfolders of my new area are ever searched:
Upvotes: 0
Views: 49
Reputation: 13435
Your URL will need to contain the name of your area. It should look something like http://<SERVERNAME>/Governance/ChangeDashboard
Upvotes: 1