Asp.net mvc 我可以在与基本控制器具有相同签名的ActionResult上使用方法隐藏吗?
我有两个控制器,一个继承另一个。我需要重写来自基本控制器的ActionResult,因为我需要更改为代码以在nopCommerce插件中实现分页。但是,由于新的ActionResult,我得到了一个模糊的MatchException 基本控制器:Asp.net mvc 我可以在与基本控制器具有相同签名的ActionResult上使用方法隐藏吗?,asp.net-mvc,nopcommerce,Asp.net Mvc,Nopcommerce,我有两个控制器,一个继承另一个。我需要重写来自基本控制器的ActionResult,因为我需要更改为代码以在nopCommerce插件中实现分页。但是,由于新的ActionResult,我得到了一个模糊的MatchException 基本控制器: public class BaseController : Controller { public ActionResult Category(int categoryId, CatalogPagingFilteringModel comman
public class BaseController : Controller {
public ActionResult Category(int categoryId, CatalogPagingFilteringModel command)
{
//original action result code
}
}
带继承的客户控制器
public class CustomController : BaseController {
public new ActionResult Category(int categoryId, CatalogPagingFilteringModel command)
{
// my pagination code with different model/view
}
}
路线信息:
在这里,我删除了基本控制器的路由,并添加了一个新路由以使用CustomCatalog控制器
routes.Remove(routes["OriginalCategory"]);
routes.MapLocalizedRoute(
"OriginalCategory",
"Category/{categoryId}/{SeName}",
new { controller = "CustomCatalog", action = "Category", SeName = UrlParameter.Optional },
new { categoryId = @"\d+" },
new[] { "Nop.Plugin.Common.Web.Controllers" });
然后我得到一个模糊的异常
[含糊不清的匹配例外:上的当前行动请求“类别”
控制器类型“CustomCatalogController”在
以下操作方法:System.Web.Mvc.ActionResult类别(Int32,
类型上的Nop.Web.Models.Catalog.CatalogPagingFilteringModel)
Nop.Plugin.Common.Web.Controllers.CustomCatalogController
System.Web.Mvc.ActionResult类别(Int32,
类型上的Nop.Web.Models.Catalog.CatalogPagingFilteringModel)
Nop.Web.Controller.CatalogController]
编辑
基本控制器位于应用程序的核心,其中CustomController位于插件中,这意味着我无法修改基本控制器的类型。在基本控制器中使用
virtual
,在派生控制器中使用override
,而不是new
,怎么样
基数:
派生:
public override ActionResult Category(...) { }
您不能重写尚未声明的方法
virtual
您可以使用不同的签名定义新方法,也可以通过在包装器类中维护对原始类的私有引用来封装原始类的功能。如果您有一个可以从基库实现的接口(因为这允许您在将使用实现相同接口的基类的地方替换包装类),那么它会有所帮助,但是您也可以在没有接口的情况下完成
// Base class source code is not modifiable
class BaseClass {
public ActionResult Category(...) {}
public ActionResult Other() {}
}
// Wrapper class can modify the behavior
class Wrapper {
private BaseClass baseClass = new BaseClass(); // Instantiate appropriately
public ActionResult Category(...) {
// do some stuff
}
public ActionResult Other() {
return baseClass.Other();
}
}
不幸的是,我无法更改基本ActionResult的类型,因为它是核心的一部分,而不是插件的一部分。
// Base class source code is not modifiable
class BaseClass {
public ActionResult Category(...) {}
public ActionResult Other() {}
}
// Wrapper class can modify the behavior
class Wrapper {
private BaseClass baseClass = new BaseClass(); // Instantiate appropriately
public ActionResult Category(...) {
// do some stuff
}
public ActionResult Other() {
return baseClass.Other();
}
}