如何在apicontroller中重复使用方法



我有一个称为车库的对象,可以具有许多车辆对象。在Apicontroller" GaragesController"中,我有以下方法:

   [ProducesResponseType(typeof(IList<VehicleResponse>), 200)]
   [HttpGet("{GarageId}/vehicles")]
    public async Task<IActionResult> GetGarageVehicles([FromQuery] FindVehiclesByGarageQuery query)
    {
        return Ok(await _findVehiclesByGarageQueryHandler.Handle(query));
    }
    [ProducesResponseType(typeof(VehicleResponse), 200)]
    [HttpGet("{garageid}/vehicles/{vehicleid}")]
    public async Task<IActionResult> GetDepotGarage(int garageid, int vehicleid)
    {
        return Ok(await _findVehicleByGarageQueryHandler.Handle(new FindVehicleByGarageQuery
        {
            GarageId= garageid,
            VehicleId = vehicleid
        }));
    }

我还有一个称为仓库的Obejct,可以包含许多车库。在即将到来的DepotsController中,是否有任何方法可以"重复使用" GarageController中的方法,或者我必须重新使用它?

您可以将可重复使用的代码封装到一种方法中,并将其放入可供控制器访问的类中。

此后,您可以根据需要在操作方法的主体内使用不同控制器中的特定路线创建单独的操作方法,并调用可重复使用的方法(放置在该单独的类中(。

这样,您可以重复使用代码,并使不同控制器的路线和操作保持不同。

最新更新