Web API 2.2,带有路由覆盖的继承控制器(这是否可能)? [英] Web API 2.2, Inherited Controller with Route Overrides (Is this possible)?
问题描述
所以我有一个通用的基本控制器实现CRUD,它来源于 APIController
。
So I have a generic base controller implementing CRUD, which derives from APIController
.
public class GenericController<TEntity> : ApiController where TEntity : class {
private readonly GenericModel<TEntity> _model;
public IModel Model {
get { return _model; }
}
public GenericController(IGenericModel<TEntity> model) {
_model = (GenericModel<TEntity>)model;
}
[HttpPost, Route]
public IHttpActionResult Post(TEntity entity) {
TEntity newEntity = _model.Create(entity);
String urlLink = $"{Request.RequestUri}{RequestContext.VirtualPathRoot}{((IGenericEntity)newEntity).ID}";
return Created(urlLink, newEntity);
}
[HttpGet, Route]
public IHttpActionResult GetList() {
return Ok(_model.ReadList());
}
[HttpGet, Route("{ID:int}")]
public IHttpActionResult Get(Int64 ID) {
return Ok(_model.Read(ID));
}
[HttpPut, Route]
public IHttpActionResult Put(TEntity entity) {
_model.Update(entity);
return Ok();
}
[HttpDelete, Route("{ID:int}")]
public IHttpActionResult Delete(Int64 ID) {
_model.Delete(ID);
return Ok();
}
}
CustomDirectRouteProvider
如下:
config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());
我的CustomDirectRouteProvider是:
Where my CustomDirectRouteProvider is:
public class CustomDirectRouteProvider : DefaultDirectRouteProvider {
protected override IReadOnlyList<IDirectRouteFactory> GetActionRouteFactories(HttpActionDescriptor actionDescriptor) {
return actionDescriptor.GetCustomAttributes<IDirectRouteFactory> (inherit: true);
}
}
这一切都很漂亮,直到我想覆写方法如POST。例如:
And this all works beautifully, until I want to override methods like POST. As an example:
[RoutePrefix(@"api/Test")]
public class DerivedController : GenericController<ConcreteEntity> {
public DerivedController (IGenericModel<ConcreteEntity> model) : base(model) {
}
[HttpPost, Route]
public new IHttpActionResult Post(ConcreteEntity entity) {
//New Post Functionality Here
}
}
此时会出现错误:
"Multiple actions were found that match the request"
所以我的问题是,如何覆盖路由本身?我基本上希望我的新的POST优先于通用的。我想通过隐藏的通用路由/方法,这只是工作,但是唉,我在我的想法是不正确的。
So my question is, how can I override the route itself? I basically want my new POST to take precedence over the generic one. I thought by virtue of the generic route/method being hidden that this would just work, but alas I was incorrect in my thinking.
推荐答案
p>能够重现您的问题,通过试验和错误的过程能够使 GenericController
一个抽象类
Was able to recreate your issue, and through a process of trial and error was able to get it to work by making GenericController
an abstract class
public abstract class GenericController<TEntity> : ApiController where TEntity : class {
[HttpPost, Route]
public virtual IHttpActionResult Post(TEntity entity) {
TEntity newEntity = _model.Create(entity);
String urlLink = $"{Request.RequestUri}{RequestContext.VirtualPathRoot}{((IGenericEntity)newEntity).ID}";
return Created(urlLink, newEntity);
}
}
并删除 DerivedController
,因为它们与继承的类匹配
and removing the attributes on the inherited action of the DerivedController
as they matched the inherited class
[RoutePrefix(@"api/Test")]
public class DerivedController : GenericController<ConcreteEntity> {
public DerivedController (IGenericModel<ConcreteEntity> model) : base(model) {
}
public override IHttpActionResult Post(ConcreteEntity entity) {
//New Post Functionality Here
}
}
这篇关于Web API 2.2,带有路由覆盖的继承控制器(这是否可能)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!