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 AreasEvernoteViews is not being searched.

How do I troubleshoot this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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();

Method 2

Be careful with AreaRegistration.RegisterAllAreas(); inside Application_Start method.

If you put AreaRegistration.RegisterAllAreas() to be last inside Application_Start that will not work.

Put AreaRegistration.RegisterAllAreas() to be first and routing will be successfully executed..

Example:

 protected void Application_Start(object sender, EventArgs e)
 {
        AreaRegistration.RegisterAllAreas();  //<--- Here work

        FilterConfig.Configure(GlobalFilters.Filters);
        RouteConfig.Configure(RouteTable.Routes);

        AreaRegistration.RegisterAllAreas();  //<--- Here not work
 }

Method 3

Like you found in my post at http://legacy.piranhacms.org/the-magic-of-mvc-routing-with-multiple-areas you probably figured out the all controllers are mapped to the default route (i.e the one you added manually in your route config). If it has been added to the default route, then it will search the location for the default route for its views, i.e ~/Views/...

So the error really seems to be that the Area isn’t configured properly. Make sure that you have the following line in your Global.asax.xs:

AreaRegistration.RegisterAllAreas();

This is the line that actually sets up the areas and makes sure that when a controller within a area is hit, the view directory of that area is searched, in your case ~/Areas/Evernote/Views. The thing covered in my blog post was how to eliminate that controllers from your Evernote area are being mapped in the default route.

Hope this help!

Regards

Håkan

Method 4

In my case the order of the configuration in Application_Start of global.asax.cs was

AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);

Changing the order made it work.

GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x