简体   繁体   English

WebApi2属性路由继承了控制器

[英]WebApi2 attribute routing inherited controllers

I'm trying to create basic REST api with a base controller like so: 我正在尝试使用基本控制器创建基本的REST api,如下所示:

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 calling GET on the route ~/api/TimesheetTask/ results in a 404 not found. 但是在路由〜/ api / TimesheetTask /上调用GET导致找不到404。

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? 所以我的问题是,如何为所有域模型编写一致的API而无需复制和粘贴代码?

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

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? 我也可以删除routeTemplate的{action}部分,但是我将如何路由到自定义操作?

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. 此外,我的域模型API的下一步将涉及支持查询,这很容易变得复杂。 Is there a library that generates these routes for you? 有没有为您生成这些路线的图书馆? If anyone can help me find such a library it would be much appreciated. 如果有人可以帮我找到这样的图书馆,我将不胜感激。

The answer you quoted has since been updated. 你引用的答案已经更新了。 As of WebApi 2.2 they created an extensibility point to allow the feature you wanted. 从WebApi 2.2开始,他们创建了一个扩展点,以允许您想要的功能。 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. 我对基本API控制器有相同的要求,并且在搜索后遇到了您引用的相同答案。

.NET WebAPI Attribute Routing and inheritance .NET WebAPI属性路由和继承

You need to overwrite the DefaultDirectRoutePrivider : 您需要覆盖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);
    }
}

With that done you then need to configure it in your web api configuration 完成后,您需要在web api配置中进行配置

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM