我有几节课
Square : Rectangle : Shape (abstract)
我有一个从 ApiController 继承的基本控制器,我想使用它。
public abstract class BaseController<T> : ApiController where T : class
{
public abstract IEnumerable<T> Get()
...
}
和
public class DerivedController : BaseController<Rectangle>
{
public override IEnumerable<Rectangle> Get()
...
}
public class AnotherDerivedController : BaseController<Square>
{
public new IEnumerable<Square> Get()
...
}
/api/rectangle 将正确调用IEnumerable<Rectangle> Get()
/api/square 会给我一个错误:
Multiple actions were found that match the request:
System.Linq.IEnumerable`1[Square] Get() on type Web.Api.Controllers.AnotherDerivedController
System.Linq.IEnumerable`1[Rectangle] Get() on type Web.Api.Controllers.DerivedController
如果我更改public new IEnumerable<Square> Get()
为public override IEnumerable<Square> Get()
,我会收到编译时错误,因为返回签名不同
如何让我的代码调用正确的方法?是否需要在 RegisterRoutes 中显式注册每个类的方法?