2015-03-03 154 views
0

我有一個基本的API控制器,這裏是一個片段:無法覆蓋ApiController方法

public abstract class CrudController<TDto, TAdd, TEdit, TLookup> : BaseApiController 
     where TDto : BaseDto, new() 
     where TAdd : BaseModel 
     where TEdit : BaseModel 
     where TLookup : BaseModel 
    { 
     [NonAction] 
     protected virtual Expression<Func<TDto, bool>> CreateWhere(Guid? id) 
     { 
      return dto => true; 
     } 

     [HttpGet] 
     public virtual DataSet<TLookup> Query([FromUri] DtoQuery query, Guid? id = null) 
     {   
      DataSet<TDto> data = DataService.Query(query, CreateWhere(id)); 
      return new DataSet<TLookup>(data.Records.Map<TLookup>(), data.Total); 
     } 
    } 

我從這個ApiController類繼承,然後希望重寫一個方法,在派生類中:

public class ProductCategoryController : CrudController<ProductSubcategoryDto, ProductSubcategoryModel, ProductSubcategoryModel, ProductSubcategoryModel> 
{ 
    protected override Expression<Func<ProductSubcategoryDto, bool>> CreateWhere(Guid? id) 
    { 
     return dto => dto.ProductCategoryGuid == id; 
    } 
} 

但在派生類中的方法沒有被執行。

獲取API/productcategorycontroller /查詢?queryetctectec從視圖中調用。

數據集<>()是在基類執行。

然後我通過代碼,該方法的第一行調用CreateWhere()和F11帶我幾行向上,而不是在派生類重寫的方法。

BaseApiController僅僅是從ApiController派生的空類。

+0

是否執行基類中的方法?完全執行「CreateWhere」函數的* call *函數嗎? – 2015-03-03 00:30:57

+0

什麼是「BaseApiController」? – 2015-03-03 00:33:53

+0

你有沒有試過不使用你的空班?嘗試直接使用'ApiController' – 2015-03-03 00:58:09

回答

0

原來我打破了錯誤的ApiController,並且它一直工作。