2

This is sort of a duplicate of Trouble setting a default controller in MVC 2 RC Area

But his answer doesn't satisfy me, because it doesn't work.

I have the following

/Areas/TestArea/Controllers/HelloController
/Areas/TestArea/Views/Hello/Index

/Controllers/HomeController
/Views/Home/Index

With the following routes:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
    "Default2", // Route name
    "TestArea/{controller}/{action}/{id}", // URL with parameters
    new { controller = "Hello", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

I added the second one to try and get http://servername/TestArea to work as if it were http://servername/TestArea/Hello but was met with no success. The basic http://servername/ works as intended.

So the question is: how do you return a default controller in an area?

Edit: I have uploaded a sample project to show what I mean: http://beginningasp.net/TestAsync.zip

Community
  • 1
  • 1
Krisc
  • 1,357
  • 2
  • 14
  • 22

1 Answers1

3

Try to register Default2 route before the default route and set area=yourareaname in the default values

routes.MapRoute(
    "Default2", // Route name
    "TestArea/{controller}/{action}/{id}", // URL with parameters
    new { controller = "Hello", action = "Index",area="TestArea",  id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Gregoire
  • 24,219
  • 6
  • 46
  • 73
  • A good, solid point. After doing so I get the following: "The view 'Index' or its master was not found. The following locations were searched:" with a list of all the root view folders. None of the area's folders were searched. – Krisc Apr 14 '10 at 18:56
  • @Krisc do you use mvccontrib or other lib for your routing? – Gregoire Apr 14 '10 at 19:05
  • Nope, this is a "blank" MVC2 project (for testing). – Krisc Apr 14 '10 at 19:12
  • Do you register your areas in you global.asax? – Gregoire Apr 14 '10 at 19:15
  • Yes. It turns out that the area registration was done first, so it was using that route which does not have the default controller! – Krisc Apr 14 '10 at 19:20
  • In MVC4 "Default" route declaraton moved from Global.asax to ~/App_Start/RouteConfig.cs/RegisterRoutes() – Andriy F. Dec 15 '12 at 19:20