Howiecamp Howiecamp - 4 months ago 117
ASP.NET (C#) Question

Getting error 404 not found with ASP.NET MVC Area routing

I am having a problem with an Area route in MVC 5. When I browse to /Evernote/EvernoteAuth I get a 404 resource cannot be found error.

My area looks like this:

Areas
Evernote
Controllers
EvernoteAuthController
Views
EvernoteAuth
Index.cshtml


The EvernoteAreaRegistration.cs (UPDATE: RegisterArea() wasn't being called so I did a Clean and Rebuild. Now it is called but same result.) contains this route map:

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


The EvernoteAuthController's Index() method simply returns View().

My application's RouteConfig.cs currently has no route maps defined, but I tried manually "forcing" it here by implementing this:

routes.MapRoute(
name: "EvernoteAuthorization",
url: "Evernote/{controller}/{action}",
defaults: new { controller = "EvernoteAuth", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "AysncOAuth.Evernote.Simple.SampleMVC.Controllers" }
);


but I get the same results whether this route map exists or is commented out.

Using Phil Haack's asp.net mvc routing debugger I saw that my routes matched fine and the area name, controller name and Action method names matched. I put breakpoints in the controller action methods and those methods were never entered. UPDATE: Those methods were never entered when browsing to /Evernote/EvernoteAuth however when I browsed to just the area name, /Evernote, an EvernoteAuthController was instantiated and the Index() method was called. (Why is that controller being instantiated by /Evernote by not by /Evernote/EvernoteAuth?) Then I received the error:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/EvernoteAuth/Index.aspx
~/Views/EvernoteAuth/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/EvernoteAuth/Index.cshtml
~/Views/Shared/Index.cshtml
and so on...


In this case I believe ~ = / (application root). So the area
Areas\Evernote\Views
is not being searched.

How do I troubleshoot this?

Answer

It is important tha you add the correct namespace to your controller

  namespace YourDefaultNamespace.Areas.Evernote.Controllers
  {
    public class EvernoteAuthController : Controller
    { 
        ...
        ...
    }
  }

So the routing can find your controller. Now you have to register the area in the Global.asax.cs with the method

AreaRegistration.RegisterAllAreas();