如何定義一組允許我處理這兩種類型的URI請求的路由(或修改我的控制器)?請注意,我可以修改我的類和路由,但我無法更改URI的結構。如何處理這種類型的Web API 2路由方案?
實施例1個URI 路由正確地到WidgetsController
API /部件/ {微件ID}
實施例2條URI 路線錯誤地到WidgetsController
。我想讓它路由到WidgetActionController
。
API /部件/ {控件ID}/widgetaction/{窗口小部件操作ID}
這裏是我的控制器
Public Class WidgetsController
Inherits ApiController
' GET api/widgets/{widget id}
Public Function [Get](id as integer) As Widget
Return DataAccess.GetWidgetByID(id)
End Function
End Class
Public Class WidgetActionController
Inherits ApiController
' GET api/widgets/{widget id}/widgetaction/{action}
Public Function [Get](id as integer, action as integer) As Boolean
'Get Widget By ID
'Perform {action} on Widget
End Function
End Class
這裏是我的路線:
config.Routes.MapHttpRoute("Data",
"api/{controller}/{id}/{year}/{month}/{day}",
New With {Key .id = RouteParameter.[Optional],
.year = RouteParameter.[Optional],
.month = RouteParameter.[Optional],
.day = RouteParameter.[Optional]})
config.Routes.MapHttpRoute("WidgetAction",
"api/widgets/{id}/widgetaction/{action}",
New With {Key .controller = "WidgetAction"})