【发布时间】:2016-07-16 11:00:31
【问题描述】:
我正在使用 Web API 为类似 Twitter 的网站构建 API,但在映射路线时遇到了问题
我对用户控制器有以下操作:
public User Get(string firstname, string lastname)
public User Get(Guid id)
public User Friends(Guid id)
public User Followers(Guid id)
public User Favorites(Guid id)
所需的路线和生成的文档应该是:
api/users?firstname={firstname}&lastname={lastname}
api/users/{id}
api/users/{id}/friends
api/users/{id}/followers
api/users/{id}/favorites
在 WebApiConfig.cs 我有:
config.Routes.MapHttpRoute(
"2",
"api/{controller}/{id}",
new { action = "get", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
"1",
"api/{controller}/{id}/{action}"
);
如何正确映射 WebAPI 路由?
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api-routing