9

我正在尝试使用 MVC 应用程序中的区域,我希望默认路由将解析为管理区域中的 HomeController,但它解析为根站点中的主控制器。我添加了管理员 HomeController 的命名空间,但它仍然解析为根 HomeController。

我的路线配置:

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"}

        );
    }
}

管理区域路线

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 - 管理区域

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

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

   }
}

知道为什么它不能正确解决吗?谢谢

4

8 回答 8

39

最直接的方法是将数据令牌添加到您的默认路由:

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

只需添加

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

到默认路由定义的末尾。

于 2013-12-26T18:48:08.273 回答
9

我已经测试了您的区域注册代码,它可以工作并选择正确的控制器。但是,即使使用了正确的控制器,视图解析也会在根文件夹中找到视图。

为了测试,我在我的区域家庭控制器中使用了以下家庭索引操作:

public ActionResult Index()
{
    return View(model: "Admin area home controller");
}

然后我在 /Views/Home 中的 index.chstml:

Root View: @Model

和我在 /Areas/Admin/Views/Home 中的 index.cshtml:

Admin Area view: @Model

运行时,输出为:

根视图:管理区域主控制器

因此该路由使管理区域中的主控制器运行,但随后视图解析继续并找到根视图而不是管理视图。

所以最后,确实是视图选择错误,所以你的问题和How to set a Default Route (To an Area) in MVC 中的问题一样。

于 2013-06-23T08:09:57.063 回答
6

此解决方案将破坏您的 API 路由。每个区域都必须有一个唯一的名称,就像默认区域一样:

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

默认路由的正确解决方案如下:

网区路线:

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

将用户重定向到“公共”区域的主要路线:

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");
于 2014-02-11T20:00:54.467 回答
2

尝试这样做....这将有助于区分。即使您不添加 area="Web",也可以

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;

同样的方法

context.MapRoute(
    "Admin_default",
    "Admin/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", area = "Admin", id = "" },
    new[] { "Nop.Admin.Controllers" }
);
于 2016-04-15T19:50:36.467 回答
1

在您的默认根主控制器中添加 Index 操作方法。在 Index action 方法中使用“return redirecttoaction”语句进行默认区域重定向

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

默认路由应该是

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {controller = "Home", 
action = "Index", id = UrlParameter.Optional}
                );
于 2016-01-04T10:01:17.223 回答
0

从 AreaRegistration 页面从区域路由的最简单方法:

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" });
于 2015-06-10T16:12:39.797 回答
0

在你的

路由配置.cs

routes.MapRoute("redirect all other requests", "{*url}",
        new {
            controller = "UnderConstruction",
            action = "Index"
            }).DataTokens = new RouteValueDictionary(new { area = "Shop" });
于 2016-01-06T14:26:21.167 回答
0

默认路由是 MVC 中的默认路由将首先调用,因为在 MVC 中首先指定了哪个路由,它将像这样调用

于 2017-09-21T13:43:49.780 回答