尽管在网站上找到了所有其他帖子,我还是很困惑。
我的解决方案有 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/