Is the ASP.NET MVC3 zone controller accessible from global routes?

I may not understand how MVC works, but it puzzled me a bit.

  • Add an area called "MyArea" by right-clicking the "Add Area" in Visual Studio in the MVC3 project.
  • Create a controller for MyArea: "AnArea" with the appropriate view in the MyArea area.
  • Add "controller =" AnArea "to the default context.MapRoute parameter in the MyAreaAreaRegistration.RegisterArea method.

So, at this point, if you run the application and go to / MyArea /, it should load the AnArea controller with the appropriate view. If you go to / MyArea / AnArea, it will show the same result.

But if you go to / AnArea /, the controller will still be found and the following error message will appear:

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

Is this the right behavior? I would think that the area manager can only be accessed through its own area, and not globally.

+5
source share
3 answers

Whenever I create a project with areas, I change the route Defaultas follows:

    routes.MapRoute( 
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // defaults
        null,  // constraints
        new string[] { "MyApplication.Controllers" } // namespaces
    );

The last parameter restricts the default route to controllers in the namespace MyApplication.Controllers. This ensures that the default route is limited to activities outside of any areas.

UPDATE

, , . :

routes.Add(
    "Default", 
    new Route("{controller}/{action}/{id}",
        new RouteValueDictionary(
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        ),
        null,
        new RouteValueDictionary(
            new {
                Namespaces = new string[] { "MyApplication.Controllers" },
                UseNamespaceFallback = false 
            }
        ),
        new MvcRouteHandler()
    )
);

UseNamespaceFallback. .

, , , , . aspnet.codeplex.com. , MVC.

+6

, .

global.asax.cs RegisterRoutes ,

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   routes.MapRoute(
      "Default", 
      "{controller}/{action}/{id}", 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      new string[] { "MyProject.Controllers" }
   );
}

"//" "MyProject.Controllers"

, "//" "MyProject.Areas.MyArea.Controllers"

"RegisterArea" "MyAreaAreaRegistration.cs", ( "MyAreaRegistration.cs" "/MyProject/Areas/MyArea" ):

//Some default code stuff
...
public override void RegisterArea(AreaRegistrationContext context)
{
   context.MapRoute(
      "MyArea_default",
      "MyArea/{controller}/{action}/{id}",
      new { action = "Index", id = UrlParameter.Optional },
      new string[] { "MyProject.Areas.MyArea.Controllers" }
   );
}

, !

0

, /AnArea, MyArea, /MyArea/. , :

context.MapRoute(
    "MyArea_default",
    "MyArea/{controller}/{action}/{id}",
    new { controller = "AnArea", action = "Index", id = UrlParameter.Optional }
);

AnArea - , . , URL MyArea, .

0

All Articles