【发布时间】:2012-04-18 22:23:06
【问题描述】:
我有几节课
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 中显式注册每个类的方法?
【问题讨论】:
标签: c# asp.net-mvc-4 asp.net-web-api