开发者

Default Controller for an Area?

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 } // Paramete开发者_开发百科r 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


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
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜