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

扶醉桌前 提交于 2019-12-30 04:31:27

问题


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?


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



回答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
 }



回答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



来源:https://stackoverflow.com/questions/25450549/getting-error-404-not-found-with-asp-net-mvc-area-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!