【发布时间】:2015-02-03 13:33:36
【问题描述】:
我自己在区域和控制器方面有点问题。 我有一个名为“HomeController”的控制器,这是默认控制器,然后我添加了一个名为“Supplier”的新区域,然后我复制了我的 HomeController 并删除了所有我不想编辑的方法,只编辑了我的 Index 方法。
现在,当我构建它时,它工作正常,但是当我作为供应商使用我的家庭控制器时,它会出现此错误
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:
TestProject.Controllers.Home
TestProject.Areas.Supplier.Controllers.Home
我已经像这样更新了我的区域
这是我的默认区域
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "TestProject.Controllers" }
);
}
}
这是我的区域路线文件
public class SupplierAreaRegistration: AreaRegistration
{
public override string AreaName
{
get
{
return "Supplier";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"SupplierHomeIndex",
"Home/Index/{id}",
defaults: new { area = "Supplier", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "TestProject.Areas.Supplier.Controllers" },
constraints: new { permalink = new AreaConstraint(AreaName) }
);
context.MapRoute(
"SupplierDefault",
"{controller}/{action}/{id}",
defaults: new { area = "Supplier", action = "index", id = UrlParameter.Optional },
namespaces: new[] { "TestProject.Controllers"},
constraints: new { permalink = new AreaConstraint(AreaName) }
);
}
}
任何人都可以对此签名吗?我已经通过 Google 和 Stackoverflow 查看了许多主题和答案,但似乎没有什么对我有用。
【问题讨论】:
-
将
RegisterRoutes中的namespaces: new[] { "TestProject.Controllers" }更新为namespaces: new[] { "TestProject.Areas.Supplier.Controllers" } -
这不会消除我的供应商限制吗?
-
您的操作链接是否定义了区域?
@Url.Action("action", "controller", new {area = "areaname"})? -
我有,但似乎仍然出现同样的错误
标签: c# asp.net-mvc asp.net-mvc-areas