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

前端 未结 4 720
春和景丽
春和景丽 2021-01-02 06:46

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:

相关标签:
4条回答
  • 2021-01-02 07:04

    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
     }
    
    0 讨论(0)
  • 2021-01-02 07:09

    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

    0 讨论(0)
  • 2021-01-02 07:18

    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();
    
    0 讨论(0)
  • 2021-01-02 07:18

    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();
    
    0 讨论(0)
提交回复
热议问题