MVC4 Default route when using areas

2020-05-21 04:34发布

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 in root site. I added the namespace of the admin HomeController but it still resolved to the root HomeController.

My Route config:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new {controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] {"MvcApplicationAreas.Areas.Admin.Controllers"}

        );
    }
}

admin area route

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}

HomeController - Admin area

namespace MvcApplicationAreas.Areas.Admin.Controllers
{
   public class HomeController : Controller
   {

       public ActionResult Index()
       {
           return View();
       }

   }
}

Any idea why it wouldn't resolve properly? Thanks

8条回答
我想做一个坏孩纸
2楼-- · 2020-05-21 04:57

Try doing this way....it will help in distinguish.Even if you dont add area="Web", its fine

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", area="Web", id = UrlParameter.Optional },
    new[] { "Nop.Web.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;

Same way

context.MapRoute(
    "Admin_default",
    "Admin/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", area = "Admin", id = "" },
    new[] { "Nop.Admin.Controllers" }
);
查看更多
Juvenile、少年°
3楼-- · 2020-05-21 04:59

Add the Index action method in your default root home controller. In the Index action method use "return redirecttoaction" statement for the default area redirection

    public ActionResult Index()
    {
        return RedirectToAction("Dashboard", "Home", new {area = "Home"});
    }

Default route should be

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {controller = "Home", 
action = "Index", id = UrlParameter.Optional}
                );
查看更多
看我几分像从前
4楼-- · 2020-05-21 05:03

The most straightforward way is to add a data token to your default route:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new {controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens.Add("area", "Admin");

Simply add

.DataTokens.Add("area", "[your area name]");

to the end of your default route definition.

查看更多
家丑人穷心不美
5楼-- · 2020-05-21 05:06

default route is by default route in MVC will call at first because in MVC which route is specified very first it will call like that

查看更多
在下西门庆
6楼-- · 2020-05-21 05:08

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");
查看更多
太酷不给撩
7楼-- · 2020-05-21 05:20

Easiest way to route from areas from AreaRegistration Page:

context.MapRoute("MainDefault", "Main/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } , new[] { "Namespace.To.Controllers" });

context.MapRoute("ClearPath", "", new { controller = "Home", action = "Index" }, new[] { "Namespace.To.Controllers" });
查看更多
登录 后发表回答