WebApi2属性继承路由控制器
我试图创建基本的REST API与基地控制器像这样:
I'm trying to create basic REST api with a base controller like so:
的基类:
public abstract class WebApiEntityController<TEntity> : ApiController
where TEntity : EntityBase<TEntity, int>
{
private readonly IRepository<TEntity> _repository;
protected WebApiEntityController(IRepository<TEntity> repository)
{
_repository = repository;
}
[Route("")]
[WebApiUnitOfWork]
public HttpResponseMessage Get()
{
return Request.CreateResponse(HttpStatusCode.OK, _repository.ToList());
}
[..........]
派生类:
[RoutePrefix("api/TimesheetTask")]
public class TimesheetTaskController : WebApiEntityController<TimesheetTask>
{
private readonly IRepository<TimesheetTask> _timeSheetTaskRepository;
public TimesheetTaskController(IRepository<TimesheetTask> timeSheetTaskRepository) : base(timeSheetTaskRepository)
{
_timeSheetTaskRepository = timeSheetTaskRepository;
}
}
但路线上调用get〜/ API / TimesheetTask /结果404没有找到。
but calling GET on the route ~/api/TimesheetTask/ results in a 404 not found.
根据这个回答,属性的路由不能被继承。所以我的问题是,我怎么能写我所有的域模型一致的API,而无需复制和粘贴code?
According to this answer, attribute routes cannot be inherited. So my question is, how can I write a consistent API for all my domain models without having to copy and paste code?
我知道我可以做约定使用此配置路由:
I know I can do convention routing with this configuration:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
但后来我不得不指定动作,而我的终点将是
but then I'll have to specify the action, and my endpoints will be
/api/{controller]/Get
/api/{controller]/Post
我不希望出现这种情况。我还可以删除routeTemplate的 {行动}
部分,但后来我将如何路线自定义操作?
and I don't want that. I can also remove the {action}
part of the routeTemplate, but then how will I route to custom actions?
如果有人可以帮助,这将是AP preciated。此外,对于我的域模型API,下一步将涉及支持查询,并可以轻松搞定复杂。有没有为你生成这些路由的图书馆吗?如果有人能帮助我找到这样的库这将是更AP preciated。
If anyone can help, that would be appreciated. Also, the next step for my domain model API would involve supporting querying, and that can easily get complicated. Is there a library that generates these routes for you? If anyone can help me find such a library it would be much appreciated.
你报的答案已经被更新。由于2.2的WebAPI他们创造了一个扩展点,让你想要的功能。
属性狂胜可以继承,但是你需要对它进行配置。我有一个基本的API控制器相同的要求,在整个后相同的答案来搜索你报价。
The answer you quoted has since been updated. As of WebApi 2.2 they created an extensibility point to allow the feature you wanted. Attribute rout can be inherited, but you need to configure it. I had the same requirement for a base API controller and after searching came across the same answer you quoted.
您需要覆盖 DefaultDirectRoutePrivider
:
public class WebApiCustomDirectRouteProvider : DefaultDirectRouteProvider {
protected override System.Collections.Generic.IReadOnlyList<IDirectRouteFactory>
GetActionRouteFactories(System.Web.Http.Controllers.HttpActionDescriptor actionDescriptor) {
// inherit route attributes decorated on base class controller's actions
return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>(inherit: true);
}
}
做完这些后,你再需要配置在Web API配置
With that done you then need to configure it in your web api configuration
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
.....
// Attribute routing. (with inheritance)
config.MapHttpAttributeRoutes(new WebApiCustomDirectRouteProvider());
....
}
}