ASP.NET MVC3 Area controller accessible from global routes?

Whenever I create an project with areas, I change my Default route as 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 final parameter limits the default route to the controllers in the MyApplication. Controllers namespace. This insures that the Default route is limited to actions outside of any areas UPDATE After a deep dive into the code, I discovered where the issue arises, and have a solution.

Change your Default route to the following: 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() ) ) The key is in adding the UseNamespaceFallback token. This will prevent the Default route from looking into any other namespaces This is unexpected behavior, and it was a problem I was unaware of which affects a project I am working on. I will list it as an issue at aspnet.codeplex.com.

I would not call this a bug, but the behavior definitely appears to breach the convetions for MVC routing.

Whenever I create an project with areas, I change my Default route as 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 final parameter limits the default route to the controllers in the MyApplication. Controllers namespace. This insures that the Default route is limited to actions outside of any areas.

UPDATE After a deep dive into the code, I discovered where the issue arises, and have a solution. Change your Default route to the following: 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() ) ); The key is in adding the UseNamespaceFallback token. This will prevent the Default route from looking into any other namespaces.

This is unexpected behavior, and it was a problem I was unaware of which affects a project I am working on. I will list it as an issue at aspnet.codeplex.com. I would not call this a bug, but the behavior definitely appears to breach the convetions for MVC routing.

This doesn't seem to work, the Area controller is still being activated via /anarea/ My Controller resides in namespace: TestAreaControllerAccessibleFromGlobal.Areas.MyArea. Controllers, and I added the namespace 'TestAreaControllerAccessibleFromGlobal. Controllers' to the Default route registration (this is where HomeController was put when creating the new project) – Duane Oct 16 at 0:25 Do you have any other routes defined in Global.asax.Cs?

– counsellorben Oct 16 at 0:34 No, only the "Default" route with the modifications you suggested in your answer: routes. MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter. Optional}, // Parameter defaults null, // constraints new { "TestAreaControllerAccessibleFromGlobal.

Controllers" } // namespaces ); – Duane Oct 16 at 0:38 The updated solution works, thanks for looking into it. I did think that it was a a bit strange behaviour ... – Duane Oct 16 at 8:28.

You have to apply a namespace restriction in both area and general route. In global.asax. Cs you should edit RegisterRoutes method just like this 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" } ); } That will restrict "//" only to the namespace "MyProject. Controllers" But also you´ll have to apply the namespace restriction to the Area to restrict "//" only to the namespace "MyProject.Areas.MyArea.

Controllers" For that you´ll have to edit "RegisterArea" method of "MyAreaAreaRegistration. Cs" like below ("MyAreaRegistration. Cs" is located at "/MyProject/Areas/MyArea" folder ) : //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" } ); } Hope it helps!

There has to be something missing, I created a new MVC3 project, added a new area called MyArea, made the above changes you suggested but the Area's controller is still visible from global ... i.e. //AreaHome invokes the /MyArea/Controllers/AreaHomeController in namespace "MyProject.Areas.MyArea. Controllers".

– Duane Oct 16 at 3:29.

You seem to be navigating to /AnArea whereas your area is called MyArea so you should navigate to /MyArea/. Here's how the area route registration looks like: context. MapRoute( "MyArea_default", "MyArea/{controller}/{action}/{id}", new { controller = "AnArea", action = "Index", id = UrlParameter.

Optional } ); AnArea is the name of the controller, not the area. If you want to navigate to some controller of this area you should always prefix your url with MyArea which is the name of the area.

I understand this, the behaviour that I'm curious about is to how to stop /AnArea/ from resolving to MyArea's AnArea controller. It appears counsellerben has found a solution, so thanks anyways – Duane Oct 16 at 8:25.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions