Howiecamp Howiecamp - 1 year ago 380
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:


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)
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:

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 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:
and so on...

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

How do I troubleshoot this?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download