c# - 区域和路由

标签 c# asp.net-mvc-3-areas

尽管在网站上找到了所有其他帖子,我还是很困惑。

我的解决方案有 Front 和 Back 两个区域,我不想使用默认提供的默认根 Controller 和 View 。

我的 FrontAreaRegistration.cs 是这样的:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Front",
        "Front/{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        , new[] { "Show.Areas.Front.Controllers" }
    );
}

我的 BackAreaRegistration.cs 是这样的:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Back_default",
        "Back/{controller}/{action}/{id}",
        new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
        , new[] { "Show.Areas.Back.Controllers" }
    );
}

Global.asax 像:

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

获取以下异常:

Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Home' has found the following matching controllers: Show.Areas.Front.Controllers.HomeController Show.Areas.Back.Controllers.HomeController

问题是我无法从前区连接到家庭 Controller 。即使将正确的命名空间添加到 context.MapRoute 方法重载 ...

任何帮助将不胜感激。

最佳答案

出现此错误是因为您未在请求中指定区域名称。由于该“默认”路由(来自 Global.asax)与请求匹配,并尝试搜索“主页” Controller 的“索引”操作。只要有两个匹配项(对于两个区域),就会抛出异常。
有几种方法可以解决这个问题。一种可能是修改 Global.asax:

routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
   new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
   new[] { "Show.Areas.Front.Controllers" }
).DataTokens.Add("Area", "Front");

但在这种情况下,“默认”路线仅适用于前区。

关于c# - 区域和路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12272535/

相关文章:

asp.net-mvc - 使用区域创建 ASP.Net MVC3 ActionLink

c# - 客户端异步 WCF 与服务器端异步 WCF

c# - NEST 批量插入后等待服务器完成索引

c# - 考虑类的多个属性的优雅排序算法

asp.net - 路由集合中已存在名为 "x"的路由。路由名称必须是唯一的。 ASP.NET MVC 3 的异常

c# - MVC 默认区域不工作

asp.net-mvc-3 - 多表单登录页面

asp.net-mvc - ASP.NET MVC] 路由匹配,但 Controller 从未调用。 -> 404

c# - Linq to SQL 使用 group By 和 order by count

c# - 帮助搜索引擎架构 .NET C#