gpt4 book ai didi

routing - 路由中的多个 Controller

转载 作者:行者123 更新时间:2023-12-04 17:37:51 24 4
gpt4 key购买 nike

我可能有一个蹩脚的问题,但我在一个 Url 中路由多个 Controller 时遇到了一些困难。所以我的问题是如何使用这样的网址:

GET http://foo.com/API/Devices/2/Parameters/2

最终在哪里 deviceparameters都是 Controller ,数字是 ID。我每个人都有 Controller ,但是如何处理它们,将它们路由到适当的 Controller ?

有什么方向可以看吗?

更新:
只是为了澄清我最终使用的解决方案并遵循以下答案。

路由片段:

config.Routes.MapHttpRoute(
name: "DeviceCommandsControllerHttpRoute",
routeTemplate: "api/devices/{deviceId}/commands/{id}",
defaults: new { controller = "devicecommands", id = RouteParameter.Optional }
);

Controller 片段:

[HttpGet]
public DeviceCommand GetById(int id, int deviceId)
{ ... }

最后是网址:
GET http://localhost:49479/api/Devices/2/Commands/1

最佳答案

我现在有类似的需求,我有以下路由结构:

public static void RegisterRoutes(HttpRouteCollection routes) {

routes.MapHttpRoute(
"UserRolesHttpRoute",
"api/users/{key}/roles",
new { controller = "UserRoles" });

routes.MapHttpRoute(
"AffiliateShipmentsHttpRoute",
"api/affiliates/{key}/shipments",
new { controller = "AffiliateShipments" });

routes.MapHttpRoute(
"ShipmentStatesHttpRoute",
"api/shipments/{key}/shipmentstates",
new { controller = "ShipmentStates" });

routes.MapHttpRoute(
"AffiliateShipmentShipmentStatesHttpRoute",
"api/affiliates/{key}/shipments/{shipmentKey}/shipmentstates",
new { controller = "AffiliateShipmentShipmentStates" });

routes.MapHttpRoute(
"DefaultHttpRoute",
"api/{controller}/{key}",
new { key = RouteParameter.Optional });
}
我认为您可以基于此弄清楚如何实现您的。

关于routing - 路由中的多个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12794029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com