2012-11-19 2 views
2

2 개의 컨트롤러가 있는데 하나는 다른 컨트롤러를 상속합니다. nopCommerce 용 플러그인에서 페이지 매김을 구현하기 위해 코드로 변경해야하기 때문에 기본 컨트롤러에서 ActionResult를 재정의해야합니다. 그러나 새로운 ActionResult 때문에 AmbiguousMatchException이 발생합니다.기본 컨트롤러와 동일한 서명이있는 ActionResult에서 메서드 숨기기를 사용할 수 있습니까?

자료 컨트롤러 : w

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" }); 

I는 다음 AmbiguousMatchException 얻을

[AmbiguousMatchException : 컨트롤러 유형 'CustomCatalogController'조치 '장르'에 대한 현재의 요구가 다음 동작 방법 간의 모호 : System.Web.Mvc 유형 Nop.Plugin.Common.Web.Controllers.CustomCatalogController System.Web.Mvc.ActionResult 카테고리 (INT32, Nop.Web에 .ActionResult 카테고리 (INT32, Nop.Web.Models.Catalog.CatalogPagingFilteringModel). 유형에 Models.Catalog.CatalogPagingFilteringModel) Nop.Web.Controllers.CatalogController]

EDIT 컨트롤러 CustomController는 I 수정할 수 없다는 것을 의미 플러그에서와 같이 어플리케이션의 코어에있는베이스 기본 컨트롤러의 유형

답변

2

당신은 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(); 
    } 
} 
4

new 대신 기본 컨트롤러에 virtual을 사용하고 파생 컨트롤러에 override을 사용하는 것은 어떻습니까?

기본 : 파생

public virtual ActionResult Category(...) { } 

:

public override ActionResult Category(...) { } 
+0

불행히도 기본 ActionResult의 유형을 플러그인의 핵심이 아니기 때문에 변경할 수 없습니다. –