c# - WebAPI controller inheritance and attribute routing -


I have some controllers that come from the same base class. They do not share with each other different things, they have something that is completely identical. I would like to put them in my base class because they all work completely, that is, they are used through different routes.

How should I define these tasks with many different routes? / Strong>

My inherited classes also have a RoutePrefixAttribute set to them so each of them points to a different path.

Example

I have a basic abstract class called vehicle and then inherited car , bikes , just etc. They will have common action move ()

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

how to do Can I define action code on my base class vehicle to execute it on every subclass route?

check the answer I answered,

what do you do is, it has been overwritten DefaultDirectRoutePrivid er :

< "pre class =" lang- CS prettyprint- override> public class WebApiCustomDirectRouteProvider: DefaultDirectRouteProvider {protected override IReadOnlyList & LT ; IDirectRouteFactory & gt; GetActionRouteFactories (HttpActionDescriptor actionDescriptor) {// Route Attribute Returns decorated on the functions of the base class controller; Descriptor.GetCustomAttributes & lt inherit; IDirectRouteFactory & gt; (Heirs: true); }}

Then you must configure your Web API configuration

  public static class WebApiConfig { public static void register (hTTP configuration config) {// ..... attribute with routing (legacy) ConfigkMapHttpAttributeRoutes (new WebApiCustomDirectRouteProvider ()); ....}}  

Then you will be able to do that which you have described as

  public abstract class VehicleControllerBase: ApiController {[root ( "move")] // all inherited classes now have a '{controller} / move` way to move public virtual HttpResponseMessage () {...}} [RoutePrefix ( "car ")] // a` car / move` route will CarController public square: VehicleControllerBase {public virtual HttpResponseMessage CarSpecificAction () {...}} [RoutePrefix ( "bike")] // Ak` bike / move` route will be public Class BikeController: Ve hicleControllerBase {public virtual HttpResponseMessage BikeSpecificAction () {...}} [RoutePrefix ( "just")] // a `bus / move` route will BusController public square: VehicleControllerBase {public virtual HttpResponseMessage BusSpecificAction () {. ..}}  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -