Areas in asp.net mvc, only one work at time

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

My folder look like this:

  • (root)/Areas/Admin/Views/..
  • (root)/Areas/Admin/Controllers/...
  • (root)/Areas/Admin/Routes.cs

  • (root)/Areas/Forum/Views/..

  • (root)/Areas/Forum/Controllers/...
  • (root)/Areas/Forum/Routes.cs

    public class Routes : AreaRegistration 

    { public override string AreaName { get { return "Admin"; } }

    public override void RegisterArea(AreaRegistrationContext context) {     context.MapRoute(         "Admin_Default",         "{controller}/{action}/{Id}",         new { controller = "Admin", action = "Index", Id = (string)null }     ); } 

    }

    public class Routes : AreaRegistration { public override string AreaName { get { return "Forum"; } }

    public override void RegisterArea(AreaRegistrationContext routes) {     routes.MapRoute(         "Forum_Default",         "{controller}/{action}",         new { controller = "Forum", action = "Index"}     ); } 

    }

Global.asax

    public static void RegisterRoutes(RouteCollection routes)     {         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");          AreaRegistration.RegisterAllAreas();          routes.MapRoute(             "Default",                                              // Route name             "{controller}/{action}/{id}",                           // URL with parameters             new { controller = "Home", action = "Index", id = "" }  // Parameter defaults         ); 

The startpage should be Home/Index but it start with Admin/Index, why?

Only site.com/Admin works not site.com/Forum

How should i get Admin and Forum Areas to work right? Why is only Admin working and not Forum?

When i delete Admin/Routes.cs file Forum start to work...

EDIT:

Home in ~/Views/ don't show as startpage even if i have

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

in my Global.asax after AreaRegistration.RegisterAllAreas();

回答1:

I believe your area mappings should be structured like so.

public override void RegisterArea(AreaRegistrationContext context) {     context.MapRoute(         "Admin_Default",         "Admin/{controller}/{action}/{Id}",         new { controller = "Admin", action = "Index", Id = (string)null }     ); } 

and

public override void RegisterArea(AreaRegistrationContext context) {     context.MapRoute(         "Forum_Default",         "Forum/{controller}/{action}/{Id}",          new { controller = "Forum", action = "Index"}     ); } 

Keeps your routes from conflicting, which is what i think is happening in your case. As your default route matches your admin route.



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