Web API 确实支持按操作路由,因此您可以执行以下操作:
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{id}/{action}",
defaults: new { action = "DirectoryInfo", id = RouteParameter.Optional }
);
控制器方法:
[HttpGet]
[ActionName("DirectoryInfo")]
public void GetDirectoryInfo(int id)
[HttpPut]
[ActionName("DirectoryInfo")]
public void PutDirectoryInfp(int id)
[HttpGet]
public void Review(int id)
[HttpPost]
[ActionName("Review")]
public void PostReview(int id)
另一种选择是创建两个控制器,BusinessController 和 ReviewController,并创建以下路由:
routes.MapHttpRoute(
name: "BusinessRoute",
routeTemplate: "api/business/{id}",
defaults: new { controller = "Business", id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "reviewsRoute",
routeTemplate: "api/contacts/{id}/reviews/",
defaults: new { controller = "Reviews", id = RouteParameter.Optional}
);
然后使用 Web API 样式的隐式操作名称。