当然,这是从Beta到RC的转变。在问题提供的示例中,您现在需要使用[HttpGet]或[AcceptVerbs(“ GET”)]装饰您的操作。
如果要将基于动词的动作(即“ GetSomething”,“ PostSomething”)与基于非动词的动作混合使用,则会导致问题。如果尝试使用上面的属性,它将与控制器中任何基于动词的操作产生冲突。引起骚动的一种方法是为每个动词定义单独的路由,并将默认操作设置为动词的名称。此方法可用于在API中定义子资源。例如,以下代码支持:“ / resource / id / children”,其中id和children是可选的。
context.Routes.MapHttpRoute(
name: "Api_Get",
routeTemplate: "{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional, action = "Get" },
constraints: new { httpMethod = new HttpMethodConstraint("GET") }
);
context.Routes.MapHttpRoute(
name: "Api_Post",
routeTemplate: "{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional, action = "Post" },
constraints: new { httpMethod = new HttpMethodConstraint("POST") }
);
希望将来版本的Web API将对此情况提供更好的支持。目前,在aspnetwebstack Codeplex项目http://aspnetwebstack.codeplex.com/workitem/184上记录了一个问题 。如果您希望看到此内容,请对该问题进行投票。