WebAPI 控制器继承和属性路由 [英] WebAPI controller inheritance and attribute routing

查看:35
本文介绍了WebAPI 控制器继承和属性路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个控制器继承自同一个基类.在它们彼此不共享的不同动作中,它们确实有一些完全相同.我希望在我的基类中包含这些,因为它们的工作方式完全相同,只是通过不同的路由访问.

I have few controllers that inherit from the same base class. Among the different actions that they don't share with each other, they do have a few that are completely identical. I would like to have these on my base class because they all work completely the same it's just that they're accessed through different routes.

我应该如何用几个不同的路由定义这些操作?

我继承的类也有一个 RoutePrefixAttribute 设置在它们上面,所以它们每个都指向不同的路由.

My inherited classes also have a RoutePrefixAttribute set on them so each of them is pointing to a different route.

我有一个名为 Vehicle 的基础抽象类,然后继承了 CarBikeBus 等等.所有的他们会有共同的动作 Move()

I have base abstract class called Vehicle and then inherited Car, Bike, Bus etc. All of them would have common action Move()

/bus/move
/car/move
/bike/move

如何在我的基类 Vehicle 上定义操作 Move() 以便它会在每个子类路由上执行?

How can I define action Move() on my base class Vehicle so that it will be executed on each subclass route?

推荐答案

查看我在这里给出的答案 WebApi2 属性路由继承控制器,它引用了这篇文章中的答案.NET WebAPI 属性路由和继承.

Check the answer I gave here WebApi2 attribute routing inherited controllers, which references the answer from this post .NET WebAPI Attribute Routing and inheritance.

你需要做的是覆盖DefaultDirectRouteProvider:

public class WebApiCustomDirectRouteProvider : DefaultDirectRouteProvider {
    protected override IReadOnlyList<IDirectRouteFactory>
        GetActionRouteFactories(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());
        ....
    }
}

然后您就可以像这样执行您所描述的操作:

You will then be able to do what you described like this:

public abstract class VehicleControllerBase : ApiController {
    [Route("move")] // All inheriting classes will now have a `{controller}/move` route 
    public virtual HttpResponseMessage Move() {
        ...
    }
}

[RoutePrefix("car")] // will have a `car/move` route
public class CarController : VehicleControllerBase { 
    public virtual HttpResponseMessage CarSpecificAction() {
        ...
    }
}

[RoutePrefix("bike")] // will have a `bike/move` route
public class BikeController : VehicleControllerBase { 
    public virtual HttpResponseMessage BikeSpecificAction() {
        ...
    }
}

[RoutePrefix("bus")] // will have a `bus/move` route
public class BusController : VehicleControllerBase { 
    public virtual HttpResponseMessage BusSpecificAction() {
        ...
    }
}

这篇关于WebAPI 控制器继承和属性路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆