【发布时间】:2019-01-10 09:29:52
【问题描述】:
我有一个基础项目和不同的继承项目。基础项目有一些控制器,我可能希望偶尔继承和覆盖(部分)。
基础项目:
public virtual ActionResult Index(string filter = "", int page = 1)
子项目:
public override ActionResult Index(string filter = "", int page = 1)
现在我更改了 routeConfig,因此路由从正确的命名空间映射到逻辑。
context.MapRoute(
"Routename",
"AreaName/{controller}/{action}/{id}",
new { controller = "ControllerName", action = "Index", id = UrlParameter.Optional },
new string[] { "ProjectName.Areas.AreaName.SpecificControllers"}
);
但是,我希望从特定项目中获取新添加的路线,如果它们存在的话。那些不存在的应该取自基础项目的控制器。 (特定的控制器基本上一开始是空的,并且只包含需要覆盖的方法)。为了尝试实现此功能,我将另一个项目添加到此处的路由中:
context.MapRoute(
"Routename",
"AreaName/{controller}/{action}/{id}",
new { controller = "ControllerName", action = "Index", id = UrlParameter.Optional },
new string[] { "ProjectName.Areas.AreaName.SpecificControllers", "ProjectName.Areas.AreaName.GenericControllers"}
);
但是,这显然会导致如下错误:
Multiple types were found that match the controller named 'MethodName'. This can happen if the route that services this request ('CRM/{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 'MethodName' has found the following matching controllers:
ProjectName.Areas.AreaName.SpecificControllers.ControllerName
ProjectName.Areas.AreaName.GenericControllers.ControllerName
有没有办法实现这一点,这样我的路由将始终首先查看特定控制器,并且如果在特定控制器中找不到方法,则仅查看通用控制器?
【问题讨论】:
标签: c# asp.net-mvc inheritance