I am trying to create a basic REST api with a basic controller as follows:
Base class:
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()); } [..........]
Derived class:
[RoutePrefix("api/TimesheetTask")] public class TimesheetTaskController : WebApiEntityController<TimesheetTask> { private readonly IRepository<TimesheetTask> _timeSheetTaskRepository; public TimesheetTaskController(IRepository<TimesheetTask> timeSheetTaskRepository) : base(timeSheetTaskRepository) { _timeSheetTaskRepository = timeSheetTaskRepository; } }
but a GET call on the ~ / api / TimesheetTask / route results in 404 not found.
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 copying and pasting the code?
I know that I can perform routing using this configuration:
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } );
but then I will need to specify the action, and my endpoints will be
/api/{controller]/Get /api/{controller]/Post
and I do not want this. I can also delete the part {action} in the routeTemplate file, but how will I route user actions?
If anyone can help, this will be appreciated. Also, the next step for my domain API is to include query support, and this can easily get complicated. Is there a library that generates these routes for you? If anyone can help me find such a library, that would be very appreciated.