WebApi2 attribute routing attributes routing

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.

+4
source share
1 answer

Your answer has since been updated. Starting with WebApi 2.2, they have created an extensibility point that allows you to use the function. The attribute path may be inherited, but you need to configure it. I had the same requirement for the base API controller, and after searching you came across the same answer that you provided.

Routing and inheriting .NET WebAPI attributes

You need to overwrite 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 actions return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>(inherit: true); } } 

After that, you need to configure it in the web api configuration

 public static class WebApiConfig { public static void Register(HttpConfiguration config) { ..... // Attribute routing. (with inheritance) config.MapHttpAttributeRoutes(new WebApiCustomDirectRouteProvider()); .... } } 
+5
source

All Articles