开发者

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

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",       开发者_JAVA技巧                                       // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜