C# 无法重写ApicController方法
我有一个基本API控制器,下面是一个片段:C# 无法重写ApicController方法,c#,generics,C#,Generics,我有一个基本API控制器,下面是一个片段: public abstract class CrudController<TDto, TAdd, TEdit, TLookup> : BaseApiController where TDto : BaseDto, new() where TAdd : BaseModel where TEdit : BaseModel where TLookup : BaseModel
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);
}
}
公共抽象类CrudController:BaseApiController
其中TDto:BaseDto,new()
其中TAdd:BaseModel
TEdit的位置:BaseModel
其中TLookup:BaseModel
{
[不行动]
受保护的虚拟表达式CreateWhere(Guid?id)
{
返回dto=>true;
}
[HttpGet]
公共虚拟数据集查询([FromUri]DtoQuery查询,Guid?id=null)
{
DataSet data=DataService.Query(Query,CreateWhere(id));
返回新的数据集(data.Records.Map(),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;
}
}
公共类ProductCategoryController:CrudController
{
受保护的重写表达式CreateWhere(Guid?id)
{
返回dto=>dto.ProductCategoryGuid==id;
}
}
但是派生类中的方法没有被执行
获取api/productcategorycontroller/query?从视图中调用QueryeTTectect
DataSet()在基类上执行
然后我一步一步地浏览代码,该方法的第一行调用CreateWhere(),F11会向上走几行,而不是到派生类中被重写的方法
BaseApiController只是从ApicController派生的一个空类。结果发现我使用了错误的ApicController,并且它一直在工作 是否改为执行基类中的方法?是否执行了对
CreateWhere
的调用?什么是BaseApiController
?是否尝试过不使用空类?尝试直接使用ApiController
是否确定您获得的是ProductCategoryController
的新实例,而不是CrudController
的实例?