我有这样的路由设置,以允许我的 webapi 控制器基于操作的路由:
config.Routes.MapHttpRoute("DefaultApiWithIdAndAction", "{controller}/{id}/{action}", null, new { id = @"\d+" });
config.Routes.MapHttpRoute("DefaultApiWithId", "{controller}/{id}", null, new {id = @"\d+"});
config.Routes.MapHttpRoute("DefaultApiWithAction", "{controller}/{action}");
config.Routes.MapHttpRoute("DefaultApiGet", "{controller}", new { action = "Get" },
new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
config.Routes.MapHttpRoute("DefaultApiPost", "{controller}", new {action = "Post"},
new {httpMethod = new HttpMethodConstraint(HttpMethod.Post)});
这是我想要支持的所有类型的路线。除了没有操作的默认 PUT 之外,它们都可以工作。出于某种原因,基于操作的 PUT 请求工作得很好。
GET users
GET users/1
POST users
PUT users/1 <- thinks its a duplicate route
PUT users/1/assignrole <- of this route even though this one works
DEL users/1
这是我定义控制器操作的方式:
public UserModel Put(int id, UserModel model)
[ActionName("assignrole")]
public UserModel PutAssignRole(int id, RoleModel model)
我会认为它们是不同的,因为动作名称不同,但 mvc 并没有这样看。我究竟做错了什么?