MVC4 Default route when using areas

前端 未结 8 1607
灰色年华
灰色年华 2021-02-03 12:30

I\'m trying to use areas within MVC app, I would like that the default route will be resolved to the HomeController within the admin area but it resolves to the home controller

8条回答
  •  南旧
    南旧 (楼主)
    2021-02-03 13:12

    This solution will broke your API route. You must have some unique name for each area, like the default area makes it:

    context.MapRoute(
             "Common_default",
             "**Common**/{controller}/{action}/{id}",
             new { action = "Index", id = UrlParameter.Optional }
    );
    

    The correct solution to default route below:

    Web area route:

    context.MapRoute(
             "Common_default",
             "Common/{culture}/{controller}/{action}/{id}",
             new {culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    

    Main route which redirect users to the "Common" area:

    routes.MapRoute(
                name: "Default",
                url: "{culture}/{controller}/{action}/{id}",
                defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "SMS.Sender.Areas.Common.Controllers" }
            ).DataTokens.Add("area","Common");
    

提交回复
热议问题